Skip to content

Commit d664e4a

Browse files
committed
docs(safe_ser): document panics if max size is too large
1 parent c78cc2d commit d664e4a

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

tfhe/src/safe_serialization.rs

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ pub struct SerializationConfig {
156156
impl SerializationConfig {
157157
/// Creates a new serialization config. The default configuration will serialize the object
158158
/// with versioning information for backward compatibility.
159-
/// `serialized_size_limit` is the size limit (in number of byte) of the serialized object
159+
/// `serialized_size_limit` is the size limit (in number of bytes) of the serialized object
160160
/// (including the header).
161161
pub fn new(serialized_size_limit: u64) -> Self {
162162
Self {
@@ -389,15 +389,16 @@ impl DeserializationConfig {
389389
///
390390
/// By default, it will check that the serialization version and the name of the
391391
/// deserialized type are correct.
392-
/// `serialized_size_limit` is the size limit (in number of byte) of the serialized object
393-
/// (include the safe serialization header).
392+
/// `deserialized_size_limit` is the size limit (in number of bytes) of the deserialized object.
393+
/// It should be set according to the expected size of the object and the maximum allocatable
394+
/// size on your system.
394395
///
395396
/// It will also check that the object is conformant with the parameter set given in
396397
/// `conformance_params`. Finally, it will check the compatibility of the loaded data with
397398
/// the current *TFHE-rs* version.
398-
pub fn new(serialized_size_limit: u64) -> Self {
399+
pub fn new(deserialized_size_limit: u64) -> Self {
399400
Self {
400-
serialized_size_limit: Some(serialized_size_limit),
401+
serialized_size_limit: Some(deserialized_size_limit),
401402
validate_header: true,
402403
}
403404
}
@@ -446,6 +447,12 @@ impl DeserializationConfig {
446447

447448
/// Deserializes an object serialized by [`SerializationConfig::serialize_into`] from a
448449
/// [reader](std::io::Read). Performs various sanity checks based on the deserialization config.
450+
///
451+
/// # Panics
452+
/// This function may panic if `serialized_size_limit` is larger than what can be allocated by
453+
/// the system. This may happen even if the size of the serialized data is short. An
454+
/// attacker could manipulate the data to create a short serialized message with a huge
455+
/// deserialized size.
449456
pub fn deserialize_from<T: DeserializeOwned + Unversionize + Named + ParameterSetConformant>(
450457
self,
451458
reader: impl std::io::Read,
@@ -479,28 +486,50 @@ pub fn safe_serialized_size<T: Serialize + Versionize + Named>(object: &T) -> bi
479486
}
480487

481488
/// Serialize an object with the default configuration (with size limit, header check and
482-
/// versioning). This is an alias for
489+
/// versioning).
490+
///
491+
/// `deserialized_size_limit` is the size limit (in number of bytes) of the deserialized object.
492+
/// It should be set according to the expected size of the object and the maximum allocatable size
493+
/// on your system.
494+
///
495+
/// This is an alias for
483496
/// `DeserializationConfig::new(serialized_size_limit).disable_conformance().deserialize_from`
497+
///
498+
/// # Panics
499+
/// This function may panic if `serialized_size_limit` is larger than what can be allocated by the
500+
/// system. This may happen even if the size of the serialized data is short. An attacker could
501+
/// manipulate the data to create a short serialized message with a huge deserialized size.
484502
pub fn safe_deserialize<T: DeserializeOwned + Unversionize + Named>(
485503
reader: impl std::io::Read,
486-
serialized_size_limit: u64,
504+
deserialized_size_limit: u64,
487505
) -> Result<T, String> {
488-
DeserializationConfig::new(serialized_size_limit)
506+
DeserializationConfig::new(deserialized_size_limit)
489507
.disable_conformance()
490508
.deserialize_from(reader)
491509
}
492510

493511
/// Serialize an object with the default configuration and conformance checks (with size limit,
494-
/// header check and versioning). This is an alias for
512+
/// header check and versioning).
513+
///
514+
/// `deserialized_size_limit` is the size limit (in number of bytes) of the deserialized object.
515+
/// It should be set according to the expected size of the object and the maximum allocatable size
516+
/// on your system.
517+
///
518+
/// This is an alias for
495519
/// `DeserializationConfig::new(serialized_size_limit).deserialize_from`
520+
///
521+
/// # Panics
522+
/// This function may panic if `serialized_size_limit` is larger than what can be allocated by the
523+
/// system. This may happen even if the size of the serialized data is short. An attacker could
524+
/// manipulate the data to create a short serialized message with a huge deserialized size.
496525
pub fn safe_deserialize_conformant<
497526
T: DeserializeOwned + Unversionize + Named + ParameterSetConformant,
498527
>(
499528
reader: impl std::io::Read,
500-
serialized_size_limit: u64,
529+
deserialized_size_limit: u64,
501530
parameter_set: &T::ParameterSet,
502531
) -> Result<T, String> {
503-
DeserializationConfig::new(serialized_size_limit).deserialize_from(reader, parameter_set)
532+
DeserializationConfig::new(deserialized_size_limit).deserialize_from(reader, parameter_set)
504533
}
505534

506535
#[cfg(all(test, feature = "shortint"))]

0 commit comments

Comments
 (0)