vhost-user: document vhost_user_read() message handling
PATCH 1/1 vhost-user: document vhost_user_read() message handling
Add a detailed documentation comment for vhost_user_read() describing its message header validation, payload size checks, and error handling.
This clarifies how complete vhost-user messages (header + payload) are read from the character device and how protocol violations are handled.
No functional changes.
--- hw/virtio/vhost-user.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)
hw/virtio/vhost-user.c +20 -1
@@ -300,6 +300,20 @@ static int vhost_user_read_header(struct vhost_dev *dev, VhostUserMsg *msg)return 0 ; } +/** +* vhost_user_read: +* @dev: The vhost device state +* @msg: Pointer to the vhost-user message structure to populate +* +* Reads a complete vhost-user message (header and optional payload) from +* the character device. It first reads and validates the header, checks +* the advertised payload size against maximum bounds, and then reads any +* attached payload directly into the message structure. +* +* Returns: 0 on success, +* -EIO or -errno on read failure or short read, +* -EPROTO if the protocol flags or payload size are invalid. +*/ static int vhost_user_read (struct vhost_dev *dev,VhostUserMsg *msg) {struct vhost_user *u = dev->opaque;
@@ -307,12 +321,13 @@ static int vhost_user_read(struct vhost_dev *dev, VhostUserMsg *msg)uint8_t *p = (uint8_t *) msg;int r, size; +/* 1. Read and validate the message header */ r =vhost_user_read_header (dev, msg);if (r <0 ) {return r; } -/* validate message size is sane */ +/* 2. Validate that the payload size is sane to prevent buffer overflows */ if (msg->hdr.size >VHOST_USER_PAYLOAD_SIZE ) { error_report("Failed to read msg header." " Size %d exceeds the maximum %zu.", msg->hdr.size,
@@ -320,9 +335,13 @@ static int vhost_user_read(struct vhost_dev *dev, VhostUserMsg *msg)return -EPROTO ; } +/* 3. If the header indicates a payload exists, read it */ if (msg->hdr.size) { +/* Advance the pointer to the start of the payload section */ p +=VHOST_USER_HDR_SIZE ; size = msg->hdr.size; + +/* Block and read the exact payload size */ r =qemu_chr_fe_read_all (chr, p, size);if (r != size) {int saved_errno = errno;
Signature · 1 line
2.34.1
Quoted 9 lines · Mohamed Ayman
Add a detailed documentation comment for
vhost_user_read()describing its message header validation, payload size checks, and error handling.This clarifies how complete vhost-user messages (header + payload) are read from the character device and how protocol violations are handled.
No functional changes.
Signed-off-by Mohamed Ayman <[email protected]>
This all seems unnecessarily verbose.
Quoted 9 lines
--- hw/virtio/vhost-user.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)hw/virtio/vhost-user.c +0
-0
hw/virtio/vhost-user.c static int vhost_user_read_header(struct vhost_dev *dev, VhostUserMsg *msg) · line 309
Quoted 10 lines
hw/virtio/vhost-user.c +7
-0@@ -300,6 +300,20 @@ static int vhost_user_read_header(struct vhost_dev *dev, VhostUserMsg *msg)return 0 ; } +/** +* vhost_user_read: +* @dev: The vhost device state +* @msg: Pointer to the vhost-user message structure to populate +* +* Reads a complete vhost-user message (header and optional payload) from +* the character device.
use imperative voice: Read ...
what "the character device"?
Quoted 3 lines
It first reads and validates the header, checks
+ * the advertised payload size against maximum bounds, and then reads any
+ * attached payload directly into the message structure.
repeating implementation here does not make sense.
Quoted 4 lines
+ *
+ * Returns: 0 on success,
+ *-EIOor -errno on read failure or short read,
+ *-EPROTOif the protocol flags or payload size are invalid.
we usually do not document specific errors.
hw/virtio/vhost-user.c static int vhost_user_read(struct vhost_dev *dev, VhostUserMsg *msg) · line 324
Quoted 9 lines
+ */
static intvhost_user_read(struct vhost_dev *dev, VhostUserMsg *msg){ struct vhost_user *u = dev->opaque;hw/virtio/vhost-user.c +1
-0@@ -307,12 +321,13 @@ static int vhost_user_read(struct vhost_dev *dev, VhostUserMsg *msg)uint8_t *p = (uint8_t *) msg;int r, size; +/* 1. Read and validate the message header */
obvious from function name
`
hw/virtio/vhost-user.c static int vhost_user_read(struct vhost_dev *dev, VhostUserMsg *msg) · line 330
Quoted 7 lines
hw/virtio/vhost-user.c +1
-1r =vhost_user_read_header (dev, msg);if (r <0 ) {return r; } -/* validate message size is sane */ +/* 2. Validate that the payload size is sane to prevent buffer overflows */
how is this better?
hw/virtio/vhost-user.c static int vhost_user_read(struct vhost_dev *dev, VhostUserMsg *msg) · line 338
Quoted 8 lines
hw/virtio/vhost-user.c +1
-0if (msg->hdr.size >VHOST_USER_PAYLOAD_SIZE ) { error_report("Failed to read msg header." " Size %d exceeds the maximum %zu.", msg->hdr.size,@@ -320,9 +335,13 @@ static int vhost_user_read(struct vhost_dev *dev, VhostUserMsg *msg)return -EPROTO ; } +/* 3. If the header indicates a payload exists, read it */
this one is ok.
hw/virtio/vhost-user.c static int vhost_user_read(struct vhost_dev *dev, VhostUserMsg *msg) · line 340
Quoted 2 lines
hw/virtio/vhost-user.c +1
-0if (msg->hdr.size) { +/* Advance the pointer to the start of the payload section */
too detailed, it's clear as is.
hw/virtio/vhost-user.c static int vhost_user_read(struct vhost_dev *dev, VhostUserMsg *msg) · line 344
Quoted 4 lines
hw/virtio/vhost-user.c +2
-0p +=VHOST_USER_HDR_SIZE ; size = msg->hdr.size; + +/* Block and read the exact payload size */
exact?
Quoted 5 lines
hw/virtio/vhost-user.c +0
-0r =qemu_chr_fe_read_all (chr, p, size);if (r != size) {int saved_errno = errno;Signature · 1 line
2.34.1