Skip to content

Commit 7214563

Browse files
committed
fix: Limit amount of memory used during snapshot deserialization
When switching from Versionize to bincode in firecracker-microvm#4230, we accidentally dropped a check limiting how much memory the deserialization routine can allocate [[1]]. This commit reimplements this check for the new bincode-based deserialization routine, with a limit matching that of the old Versionize check. [1]: https://github.com/firecracker-microvm/versionize/blob/main/src/primitives.rs#L14C33-L14C43 Signed-off-by: Patrick Roy <[email protected]>
1 parent 5f95011 commit 7214563

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/vmm/src/snapshot/mod.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ mod persist;
3030
use std::fmt::Debug;
3131
use std::io::{Read, Write};
3232

33+
use bincode::Options;
3334
use semver::Version;
3435
use serde::de::DeserializeOwned;
3536
use serde::{Deserialize, Serialize};
@@ -40,6 +41,9 @@ pub use crate::snapshot::persist::Persist;
4041
#[cfg(target_arch = "x86_64")]
4142
const SNAPSHOT_MAGIC_ID: u64 = 0x0710_1984_8664_0000u64;
4243

44+
/// Constant bounding how much memory bincode may allocate during vmstate file deserialization
45+
const VM_STATE_DESERIALIZE_LIMIT: u64 = 10_485_760; // 10MiB
46+
4347
#[cfg(target_arch = "aarch64")]
4448
const SNAPSHOT_MAGIC_ID: u64 = 0x0710_1984_AAAA_0000u64;
4549

@@ -108,7 +112,14 @@ impl Snapshot {
108112
T: Read,
109113
O: DeserializeOwned + Debug,
110114
{
111-
bincode::deserialize_from(reader).map_err(|err| Error::Serde(err.to_string()))
115+
// flags below are those used by default by bincode::deserialize_from, plus `with_limit`.
116+
bincode::DefaultOptions::new()
117+
.with_limit(VM_STATE_DESERIALIZE_LIMIT)
118+
.with_fixint_encoding()
119+
.allow_trailing_bytes() // need this because we deserialize header and snapshot from the same file, so after
120+
// reading the header, there will be trailing bytes.
121+
.deserialize_from(reader)
122+
.map_err(|err| Error::Serde(err.to_string()))
112123
}
113124

114125
/// Helper function to serialize an object to a writer

0 commit comments

Comments
 (0)