Skip to content

Commit

Permalink
vhost: fix possible FD leaks
Browse files Browse the repository at this point in the history
On failure, read_vhost_message() only closed the message
FDs if the header size was unexpected, but there are other
cases where it is required. For example in the case the
payload size read from the header is greater than the
expected maximum payload size.

This patch fixes this by closing all messages FDs in all
error cases. It also improve error logging by displaying
the request name when failure happens if possible.

Fixes: bf47225 ("vhost: fix possible denial of service by leaking FDs")
Cc: stable@dpdk.org

Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
Signed-off-by: David Marchand <david.marchand@redhat.com>
Reviewed-by: Chenbo Xia <chenbo.xia@intel.com>
  • Loading branch information
mcoquelin committed Feb 9, 2023
1 parent 43ccd55 commit 585283f
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions lib/vhost/vhost_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -2831,29 +2831,36 @@ read_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *

ret = read_fd_message(dev->ifname, sockfd, (char *)&ctx->msg, VHOST_USER_HDR_SIZE,
ctx->fds, VHOST_MEMORY_MAX_NREGIONS, &ctx->fd_num);
if (ret <= 0) {
return ret;
} else if (ret != VHOST_USER_HDR_SIZE) {
if (ret <= 0)
goto out;

if (ret != VHOST_USER_HDR_SIZE) {
VHOST_LOG_CONFIG(dev->ifname, ERR, "Unexpected header size read\n");
close_msg_fds(ctx);
return -1;
ret = -1;
goto out;
}

if (ctx->msg.size) {
if (ctx->msg.size > sizeof(ctx->msg.payload)) {
VHOST_LOG_CONFIG(dev->ifname, ERR, "invalid msg size: %d\n",
ctx->msg.size);
return -1;
ret = -1;
goto out;
}
ret = read(sockfd, &ctx->msg.payload, ctx->msg.size);
if (ret <= 0)
return ret;
goto out;
if (ret != (int)ctx->msg.size) {
VHOST_LOG_CONFIG(dev->ifname, ERR, "read control message failed\n");
return -1;
ret = -1;
goto out;
}
}

out:
if (ret <= 0)
close_msg_fds(ctx);

return ret;
}

Expand Down Expand Up @@ -3031,13 +3038,10 @@ vhost_user_msg_handler(int vid, int fd)
}
}

ctx.msg.request.master = VHOST_USER_NONE;
ret = read_vhost_message(dev, fd, &ctx);
if (ret <= 0) {
if (ret < 0)
VHOST_LOG_CONFIG(dev->ifname, ERR, "vhost read message failed\n");
else
VHOST_LOG_CONFIG(dev->ifname, INFO, "vhost peer closed\n");

if (ret == 0) {
VHOST_LOG_CONFIG(dev->ifname, INFO, "vhost peer closed\n");
return -1;
}

Expand All @@ -3047,6 +3051,14 @@ vhost_user_msg_handler(int vid, int fd)
else
msg_handler = NULL;

if (ret < 0) {
VHOST_LOG_CONFIG(dev->ifname, ERR, "vhost read message %s%s%sfailed\n",
msg_handler != NULL ? "for " : "",
msg_handler != NULL ? msg_handler->description : "",
msg_handler != NULL ? " " : "");
return -1;
}

if (msg_handler != NULL && msg_handler->description != NULL) {
if (request != VHOST_USER_IOTLB_MSG)
VHOST_LOG_CONFIG(dev->ifname, INFO,
Expand Down

0 comments on commit 585283f

Please sign in to comment.