From ead9be163d2fc0012b67b4328c5a3a0e41011ff7 Mon Sep 17 00:00:00 2001 From: Sergio Lopez Date: Wed, 23 Oct 2019 16:07:03 +0200 Subject: vhost-user: fix VhostUserConfig payload management The VhostUserConfig carries a message with a payload, the contents of which depend on the kind of device being emulated. With this change, we calculate the offset of the payload within the message, check its size corresponds to the expected one, and pass it to the backend as a reference to a slice adjusted to the payload dimensions. The backend will be responsible of validating the payload, as it's the one aware of its expected contents. Signed-off-by: Sergio Lopez --- src/vhost_user/slave_req_handler.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/vhost_user') diff --git a/src/vhost_user/slave_req_handler.rs b/src/vhost_user/slave_req_handler.rs index 934c6d4..480689f 100644 --- a/src/vhost_user/slave_req_handler.rs +++ b/src/vhost_user/slave_req_handler.rs @@ -265,7 +265,6 @@ impl SlaveReqHandler { if self.acked_protocol_features & VhostUserProtocolFeatures::CONFIG.bits() == 0 { return Err(Error::InvalidOperation); } - self.check_request_size(&hdr, size, mem::size_of::())?; self.get_config(&hdr, &buf)?; } MasterReq::SET_CONFIG => { @@ -341,6 +340,10 @@ impl SlaveReqHandler { if !msg.is_valid() { return Err(Error::InvalidMessage); } + let payload_offset = mem::size_of::(); + if buf.len() - payload_offset != msg.size as usize { + return Err(Error::InvalidMessage); + } let flags = match VhostUserConfigFlags::from_bits(msg.flags) { Some(val) => val, None => return Err(Error::InvalidMessage), @@ -519,6 +522,7 @@ impl SlaveReqHandler { fn new_reply_header( &self, req: &VhostUserMsgHeader, + payload_size: usize, ) -> Result> { if mem::size_of::() > MAX_MSG_SIZE { return Err(Error::InvalidParam); @@ -527,7 +531,7 @@ impl SlaveReqHandler { Ok(VhostUserMsgHeader::new( req.get_code(), VhostUserHeaderFlag::REPLY.bits(), - mem::size_of::() as u32, + (mem::size_of::() + payload_size) as u32, )) } @@ -537,7 +541,7 @@ impl SlaveReqHandler { res: Result<()>, ) -> Result<()> { if self.reply_ack_enabled { - let hdr = self.new_reply_header::(req)?; + let hdr = self.new_reply_header::(req, 0)?; let val = match res { Ok(_) => 0, Err(_) => 1, @@ -553,7 +557,7 @@ impl SlaveReqHandler { req: &VhostUserMsgHeader, msg: &T, ) -> Result<()> { - let hdr = self.new_reply_header::(req)?; + let hdr = self.new_reply_header::(req, 0)?; self.main_sock.send_message(&hdr, msg, None)?; Ok(()) } @@ -568,7 +572,7 @@ impl SlaveReqHandler { T: Sized, P: Sized, { - let hdr = self.new_reply_header::(req)?; + let hdr = self.new_reply_header::(req, payload.len())?; self.main_sock .send_message_with_payload(&hdr, msg, payload, None)?; Ok(()) -- cgit v1.2.3