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

Wasmer cache can throw panic if cache file has been corrupted #2955

Closed
grishasobol opened this issue Jun 14, 2022 · 9 comments · Fixed by #3130
Closed

Wasmer cache can throw panic if cache file has been corrupted #2955

grishasobol opened this issue Jun 14, 2022 · 9 comments · Fixed by #3130
Assignees
Labels
🎉 enhancement New feature! priority-medium Medium priority issue
Milestone

Comments

@grishasobol
Copy link
Contributor

/// # Safety
/// This function is unsafe because rkyv reads directly without validating
/// the data.
pub unsafe fn deserialize(
engine: &UniversalEngine,
bytes: &[u8],
) -> Result<Self, DeserializeError> {
if !UniversalArtifactBuild::is_deserializable(bytes) {
return Err(DeserializeError::Incompatible(
"The provided bytes are not wasmer-universal".to_string(),
));
}
let bytes = &bytes[UniversalArtifactBuild::MAGIC_HEADER.len()..];
let metadata_len = MetadataHeader::parse(bytes)?;
let metadata_slice: &[u8] = &bytes[MetadataHeader::LEN..][..metadata_len];
let serializable = SerializableModule::deserialize(metadata_slice)?;
let artifact = UniversalArtifactBuild::from_serializable(serializable);
let mut inner_engine = engine.inner_mut();
Self::from_parts(&mut inner_engine, artifact).map_err(DeserializeError::Compiler)

Hello guys, as I can see in code above, you work with string slice without any checks before. As a result this may cause problems in some cases. For example: if we would corrupt file in compiled wasm cache, then this part of code may throw panic, which is not suitable behaviour. I think the best way is to return error in the case.

@epilys
Copy link
Contributor

epilys commented Jun 14, 2022

Hello, thanks for your ticket!

This function is unsafe for this reason as explained in the doc comment: the byte slice is not validated. I think the best way to deal with the problem you're saying is providing an opt-in validation option.

@grishasobol
Copy link
Contributor Author

is providing an opt-in validation option.

If you mean - check bytes before calling this method, then it's not the most optimal way. Because then we have to copy some logic from this method and do it before calling it, in order to be sure that it wont throw panic.
Much simple way is if this method returns error in all cases when bytes are in inconsistent state. Even if it's unsafe method =)

@epilys
Copy link
Contributor

epilys commented Jun 14, 2022

I meant a validate: bool argument. You might not want to revalidate an immutable bytes slice after a first validation. Would that satisfy your usecase?

@grishasobol
Copy link
Contributor Author

I meant a validate: bool argument. You might not want to revalidate an immutable bytes slice after a first validation. Would that satisfy your use case.

Yep, if you will use validate = true by default in wasmer functions which uses this method, or propagate validate argument up for all functions which uses this method.

@epilys
Copy link
Contributor

epilys commented Jun 14, 2022

Propagating the option makes sense to me. We're redesigning the API for the next major release, 3.0, at the moment. So we will take this into consideration, thanks for writing the ticket!

@epilys epilys added this to the v3.0 milestone Jun 14, 2022
@epilys epilys added 🎉 enhancement New feature! priority-medium Medium priority issue labels Jun 14, 2022
@silwol
Copy link
Contributor

silwol commented Jun 15, 2022

If we strive to follow the API guidelines, I think it makes sense to rather expose a deserialize_unchecked(…) method that may panic, and a deserialize(…) method that does checking and returns a Result<…>. The latter may call the former of course, or both call an additional private function that has a validate: bool parameter - depending on what makes more sense for the implementation.

@epilys
Copy link
Contributor

epilys commented Jun 21, 2022

Tracked in #2925

@epilys epilys closed this as completed Jun 21, 2022
@epilys
Copy link
Contributor

epilys commented Jun 22, 2022

Actually keeping this open because it might get lost in #2925. But it depends on it.

@epilys epilys reopened this Jun 22, 2022
@syrusakbary
Copy link
Member

We need to make sure that deserialize that validates the data and never panics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎉 enhancement New feature! priority-medium Medium priority issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants