Skip to content

Commit 4f47ee2

Browse files
committed
miniscript/types: check that timelock values aren't too large
This changes the ZeroTime error into an InvalidTime error and checks that the disabling bit (most significant one) isn't set in the timelock value. We could also check the value is minimal, using the mask. Signed-off-by: Antoine Poinsot <[email protected]>
1 parent f550b9b commit 4f47ee2

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

src/miniscript/types/extra_props.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Other miscellaneous type properties which are not related to
22
//! correctness or malleability.
33
4-
use miniscript::limits::{HEIGHT_TIME_THRESHOLD, SEQUENCE_LOCKTIME_TYPE_FLAG};
4+
use miniscript::limits::{HEIGHT_TIME_THRESHOLD, SEQUENCE_LOCKTIME_TYPE_FLAG, SEQUENCE_LOCKTIME_DISABLE_FLAG};
55

66
use super::{Error, ErrorKind, Property, ScriptContext};
77
use script_num_size;
@@ -851,20 +851,19 @@ impl Property for ExtData {
851851
Ok(Self::from_multi(k, pks.len()))
852852
}
853853
Terminal::After(t) => {
854-
// FIXME check if t > 2^31 - 1
855-
if t == 0 {
854+
if t == 0 || (t & SEQUENCE_LOCKTIME_DISABLE_FLAG) == 1 {
856855
return Err(Error {
857856
fragment: fragment.clone(),
858-
error: ErrorKind::ZeroTime,
857+
error: ErrorKind::InvalidTime,
859858
});
860859
}
861860
Ok(Self::from_after(t))
862861
}
863862
Terminal::Older(t) => {
864-
if t == 0 {
863+
if t == 0 || (t & SEQUENCE_LOCKTIME_DISABLE_FLAG) == 1 {
865864
return Err(Error {
866865
fragment: fragment.clone(),
867-
error: ErrorKind::ZeroTime,
866+
error: ErrorKind::InvalidTime,
868867
});
869868
}
870869
Ok(Self::from_older(t))

src/miniscript/types/mod.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use std::{error, fmt};
2525
pub use self::correctness::{Base, Correctness, Input};
2626
pub use self::extra_props::ExtData;
2727
pub use self::malleability::{Dissat, Malleability};
28-
use super::ScriptContext;
28+
use super::{ScriptContext, limits::SEQUENCE_LOCKTIME_DISABLE_FLAG};
2929
use MiniscriptKey;
3030
use Terminal;
3131

@@ -38,8 +38,8 @@ fn return_none<T>(_: usize) -> Option<T> {
3838
/// Detailed type of a typechecker error
3939
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
4040
pub enum ErrorKind {
41-
/// Relative or absolute timelock had a time value of 0
42-
ZeroTime,
41+
/// Relative or absolute timelock had an invalid time value ()
42+
InvalidTime,
4343
/// Passed a `z` argument to a `d` wrapper when `z` was expected
4444
NonZeroDupIf,
4545
/// Multisignature or threshold policy had a `k` value of 0
@@ -117,9 +117,9 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> error::Error for Error<Pk, Ctx> {
117117
impl<Pk: MiniscriptKey, Ctx: ScriptContext> fmt::Display for Error<Pk, Ctx> {
118118
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
119119
match self.error {
120-
ErrorKind::ZeroTime => write!(
120+
ErrorKind::InvalidTime => write!(
121121
f,
122-
"fragment «{}» represents a 0-valued timelock (use `1` instead)",
122+
"fragment «{}» represents a timelock which value is invalid (time must be in [1; 0x80000000])",
123123
self.fragment,
124124
),
125125
ErrorKind::NonZeroDupIf => write!(
@@ -426,20 +426,19 @@ pub trait Property: Sized {
426426
Ok(Self::from_multi(k, pks.len()))
427427
}
428428
Terminal::After(t) => {
429-
if t == 0 {
429+
if t == 0 || (t & SEQUENCE_LOCKTIME_DISABLE_FLAG) == 1 {
430430
return Err(Error {
431431
fragment: fragment.clone(),
432-
error: ErrorKind::ZeroTime,
432+
error: ErrorKind::InvalidTime,
433433
});
434434
}
435435
Ok(Self::from_after(t))
436436
}
437437
Terminal::Older(t) => {
438-
// FIXME check if t > 2^31 - 1
439-
if t == 0 {
438+
if t == 0 || (t & SEQUENCE_LOCKTIME_DISABLE_FLAG) == 1 {
440439
return Err(Error {
441440
fragment: fragment.clone(),
442-
error: ErrorKind::ZeroTime,
441+
error: ErrorKind::InvalidTime,
443442
});
444443
}
445444
Ok(Self::from_older(t))
@@ -803,20 +802,19 @@ impl Property for Type {
803802
Ok(Self::from_multi(k, pks.len()))
804803
}
805804
Terminal::After(t) => {
806-
// FIXME check if t > 2^31 - 1
807-
if t == 0 {
805+
if t == 0 || (t & SEQUENCE_LOCKTIME_DISABLE_FLAG) == 1 {
808806
return Err(Error {
809807
fragment: fragment.clone(),
810-
error: ErrorKind::ZeroTime,
808+
error: ErrorKind::InvalidTime,
811809
});
812810
}
813811
Ok(Self::from_after(t))
814812
}
815813
Terminal::Older(t) => {
816-
if t == 0 {
814+
if t == 0 || (t & SEQUENCE_LOCKTIME_DISABLE_FLAG) == 1 {
817815
return Err(Error {
818816
fragment: fragment.clone(),
819-
error: ErrorKind::ZeroTime,
817+
error: ErrorKind::InvalidTime,
820818
});
821819
}
822820
Ok(Self::from_older(t))

0 commit comments

Comments
 (0)