From mboxrd@z Thu Jan 1 00:00:00 1970 From: Maxime Coquelin Subject: [PATCH v2 1/3] vhost: fix fd leak in VHOST_USER_SET_LOG_BASE Date: Fri, 24 Nov 2017 19:08:24 +0100 Message-ID: <20171124180826.18439-2-maxime.coquelin@redhat.com> References: <20171124180826.18439-1-maxime.coquelin@redhat.com> Cc: stable@dpdk.org, jfreiman@redhat.com, Maxime Coquelin To: dev@dpdk.org, yliu@fridaylinux.org, tiwei.bie@intel.com, jianfeng.tan@intel.com, vkaplans@redhat.com Return-path: In-Reply-To: <20171124180826.18439-1-maxime.coquelin@redhat.com> List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" If VHOST_USER_SET_LOG_BASE request's message size is invalid, the fd is leaked. Fix this by closing the fd systematically as long as it is valid. Fixes: 53af5b1e0ace ("vhost: fix leak of file descriptor") Cc: stable@dpdk.org Signed-off-by: Maxime Coquelin --- lib/librte_vhost/vhost_user.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c index f4c7ce462..f06d9bb65 100644 --- a/lib/librte_vhost/vhost_user.c +++ b/lib/librte_vhost/vhost_user.c @@ -895,6 +895,7 @@ static int vhost_user_set_log_base(struct virtio_net *dev, struct VhostUserMsg *msg) { int fd = msg->fds[0]; + int ret = 0; uint64_t size, off; void *addr; @@ -907,7 +908,8 @@ vhost_user_set_log_base(struct virtio_net *dev, struct VhostUserMsg *msg) RTE_LOG(ERR, VHOST_CONFIG, "invalid log base msg size: %"PRId32" != %d\n", msg->size, (int)sizeof(VhostUserLog)); - return -1; + ret = -1; + goto out; } size = msg->payload.log.mmap_size; @@ -921,10 +923,10 @@ vhost_user_set_log_base(struct virtio_net *dev, struct VhostUserMsg *msg) * fail when offset is not page size aligned. */ addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - close(fd); if (addr == MAP_FAILED) { RTE_LOG(ERR, VHOST_CONFIG, "mmap log base failed!\n"); - return -1; + ret = -1; + goto out; } /* @@ -938,7 +940,10 @@ vhost_user_set_log_base(struct virtio_net *dev, struct VhostUserMsg *msg) dev->log_base = dev->log_addr + off; dev->log_size = size; - return 0; +out: + close(fd); + + return ret; } /* -- 2.14.3