Skip to content

Commit 4f28f88

Browse files
luminitavoicudianpopa
authored andcommitted
vsock:avoid overflow on computing packet used len
Signed-off-by: Luminita Voicu <[email protected]>
1 parent ab4832d commit 4f28f88

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

src/devices/src/virtio/vsock/csm/connection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ where
237237
if err.kind() == ErrorKind::WouldBlock =>
238238
{
239239
// This shouldn't actually happen (receiving EWOULDBLOCK after EPOLLIN), but
240-
// apparently it does, so we need to handle it greacefully.
240+
// apparently it does, so we need to handle it gracefully.
241241
warn!(
242242
"vsock: unexpected EWOULDBLOCK while reading from backing stream: \
243243
lp={}, pp={}, err={:?}",

src/devices/src/virtio/vsock/device.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,14 @@ where
148148
Ok(mut pkt) => {
149149
if self.backend.recv_pkt(&mut pkt, mem).is_ok() {
150150
match pkt.commit_hdr(mem) {
151+
// This addition cannot overflow, because packet length
152+
// is previously validated against `MAX_PKT_BUF_SIZE`
153+
// bound as part of `commit_hdr()`.
151154
Ok(()) => VSOCK_PKT_HDR_SIZE as u32 + pkt.len(),
152155
Err(e) => {
153156
warn!(
154-
"vsock: Error writing packet header to guest memory: {:?}",
157+
"vsock: Error writing packet header to guest memory: {:?}.\
158+
Discarding the package.",
155159
e
156160
);
157161
0
@@ -165,7 +169,7 @@ where
165169
}
166170
}
167171
Err(e) => {
168-
warn!("vsock: RX queue error: {:?}", e);
172+
warn!("vsock: RX queue error: {:?}. Discarding the package.", e);
169173
0
170174
}
171175
};

src/devices/src/virtio/vsock/packet.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,7 @@ impl VsockPacket {
176176
}
177177

178178
// Reject weirdly-sized packets.
179-
//
180-
if pkt.len() > defs::MAX_PKT_BUF_SIZE as u32 {
181-
return Err(VsockError::InvalidPktLen(pkt.len()));
182-
}
179+
pkt.check_len()?;
183180

184181
pkt.init_buf(hdr_desc, false)?;
185182

@@ -222,10 +219,22 @@ impl VsockPacket {
222219

223220
/// Writes the local copy of the packet header to the guest memory.
224221
pub fn commit_hdr(&self, mem: &GuestMemoryMmap) -> Result<()> {
222+
// Reject weirdly-sized packets.
223+
self.check_len()?;
224+
225225
mem.write_obj(self.hdr, self.hdr_addr)
226226
.map_err(VsockError::GuestMemoryMmap)
227227
}
228228

229+
/// Verifies packet length against `MAX_PKT_BUF_SIZE` limit.
230+
pub fn check_len(&self) -> Result<()> {
231+
if self.len() > defs::MAX_PKT_BUF_SIZE as u32 {
232+
return Err(VsockError::InvalidPktLen(self.len()));
233+
}
234+
235+
Ok(())
236+
}
237+
229238
pub fn buf_size(&self) -> usize {
230239
self.buf_size
231240
}

0 commit comments

Comments
 (0)