Skip to content

Invalid TPKT size was detected #22

@HsuJv

Description

@HsuJv

Hi @citronneur ,

Thanks for your amazing work to bringing us this crate.
I'm trying to build a wasm application based on your work.
But got some unexpected error "Invalid minimal size for TPKT"
I tried to ignore it and it seems can restore itself.
But when looking through your mstsc-rs code, it seems the program shall be terminated when the error occurs

rdp-rs/src/bin/mstsc-rs.rs

Lines 325 to 344 in 7ac880d

while wait_for_fd(handle as usize) && sync.load(Ordering::Relaxed) {
let mut guard = rdp_client.lock().unwrap();
if let Err(Error::RdpError(e)) = guard.read(|event| {
match event {
RdpEvent::Bitmap(bitmap) => {
bitmap_channel.send(bitmap).unwrap();
},
_ => println!("{}: ignore event", APPLICATION_NAME)
}
}) {
match e.kind() {
RdpErrorKind::Disconnect => {
println!("{}: Server ask for disconnect", APPLICATION_NAME);
},
_ => println!("{}: {:?}", APPLICATION_NAME, e)
}
break;
}
}
}))

See the screenshot which shows it continues to work when the error shows up.
Worked but lots of error

Do you have any idea about this error? 'cuz I cannot observe it when using the mstsc-rs which makes me confused.

BTW, the errors happens at line 142 #1, 159 #2, 166 #3 with the tuple (action, length) shown in the screenshot

rdp-rs/src/core/tpkt.rs

Lines 124 to 172 in 7ac880d

pub fn read(&mut self) -> RdpResult<Payload> {
let mut buffer = Cursor::new(self.transport.read(2)?);
let mut action: u8 = 0;
action.read(&mut buffer)?;
if action == Action::FastPathActionX224 as u8 {
// read padding
let mut padding: u8 = 0;
padding.read(&mut buffer)?;
// now wait extended header
buffer = Cursor::new(self.transport.read(2)?);
let mut size = U16::BE(0);
size.read(&mut buffer)?;
// Minimal size must be 7
// https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpbcgr/18a27ef9-6f9a-4501-b000-94b1fe3c2c10
if size.inner() < 4 {
Err(Error::RdpError(RdpError::new(RdpErrorKind::InvalidSize, "Invalid minimal size for TPKT")))
}
else {
// now wait for body
Ok(Payload::Raw(Cursor::new(self.transport.read(size.inner() as usize - 4)?)))
}
} else {
// fast path
let sec_flag = (action >> 6) & 0x3;
let mut short_length: u8 = 0;
short_length.read(&mut buffer)?;
if short_length & 0x80 != 0 {
let mut hi_length: u8 = 0;
hi_length.read(&mut Cursor::new(self.transport.read(1)?))?;
let length: u16 = ((short_length & !0x80) as u16) << 8;
let length = length | hi_length as u16;
if length < 3 {
Err(Error::RdpError(RdpError::new(RdpErrorKind::InvalidSize, "Invalid minimal size for TPKT")))
} else {
Ok(Payload::FastPath(sec_flag, Cursor::new(self.transport.read(length as usize - 3)?)))
}
}
else {
if short_length < 2 {
Err(Error::RdpError(RdpError::new(RdpErrorKind::InvalidSize, "Invalid minimal size for TPKT")))
} else {
Ok(Payload::FastPath(sec_flag, Cursor::new(self.transport.read(short_length as usize - 2)?)))
}
}
}
}

Thanks in advance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions