Skip to content

Commit 5c494cd

Browse files
committed
object: Avoid crashing on zero-length AllowedMechanisms attribute
As returned by SoftHSM: softhsm/SoftHSMv2#825 Fixes: #323 Signed-off-by: Jakub Jelen <[email protected]>
1 parent 2702bba commit 5c494cd

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

cryptoki/src/object.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,18 +1114,25 @@ impl TryFrom<CK_ATTRIBUTE> for Attribute {
11141114
Ok(Attribute::ValidationVersion(Version::new(val[0], val[1])))
11151115
}
11161116
AttributeType::AllowedMechanisms => {
1117-
let val = unsafe {
1118-
std::slice::from_raw_parts(
1119-
attribute.pValue as *const CK_MECHANISM_TYPE,
1120-
attribute.ulValueLen.try_into()?,
1121-
)
1122-
};
1123-
let types: Vec<MechanismType> = val
1124-
.iter()
1125-
.copied()
1126-
.map(|t| t.try_into())
1127-
.collect::<Result<Vec<MechanismType>>>()?;
1128-
Ok(Attribute::AllowedMechanisms(types))
1117+
if attribute.ulValueLen == 0 {
1118+
/* For zero-length attributes we are getting pointer
1119+
* to static buffer of length zero, which can not be to create slices.
1120+
* Short-circuit here to avoid crash (#324) */
1121+
Ok(Attribute::AllowedMechanisms(Vec::<MechanismType>::new()))
1122+
} else {
1123+
let val = unsafe {
1124+
std::slice::from_raw_parts(
1125+
attribute.pValue as *const CK_MECHANISM_TYPE,
1126+
attribute.ulValueLen.try_into()?,
1127+
)
1128+
};
1129+
let types: Vec<MechanismType> = val
1130+
.iter()
1131+
.copied()
1132+
.map(|t| t.try_into())
1133+
.collect::<Result<Vec<MechanismType>>>()?;
1134+
Ok(Attribute::AllowedMechanisms(types))
1135+
}
11291136
}
11301137
AttributeType::EndDate => {
11311138
if val.is_empty() {

0 commit comments

Comments
 (0)