@@ -30,6 +30,7 @@ mod persist;
30
30
use std:: fmt:: Debug ;
31
31
use std:: io:: { Read , Write } ;
32
32
33
+ use bincode:: Options ;
33
34
use semver:: Version ;
34
35
use serde:: de:: DeserializeOwned ;
35
36
use serde:: { Deserialize , Serialize } ;
@@ -40,6 +41,9 @@ pub use crate::snapshot::persist::Persist;
40
41
#[ cfg( target_arch = "x86_64" ) ]
41
42
const SNAPSHOT_MAGIC_ID : u64 = 0x0710_1984_8664_0000u64 ;
42
43
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
+
43
47
#[ cfg( target_arch = "aarch64" ) ]
44
48
const SNAPSHOT_MAGIC_ID : u64 = 0x0710_1984_AAAA_0000u64 ;
45
49
@@ -108,7 +112,14 @@ impl Snapshot {
108
112
T : Read ,
109
113
O : DeserializeOwned + Debug ,
110
114
{
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 ( ) ) )
112
123
}
113
124
114
125
/// Helper function to serialize an object to a writer
0 commit comments