From nobody Wed Apr 16 13:11:28 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1603468138; cv=none; d=zohomail.com; s=zohoarc; b=TOrJYgpQwJlGipZXJPA0+T24dtedjbTm0YjN+n+Prx5n0Y4xsZEErdtq7dIu6MxREMfGn4Ow/f/TP13MhC9ZfDn+bzEw1GCe8QnDqXIHiXLPjS5XttuNfFEJf8vPI8k7rpKEjxbvgbuiL9SEdi/sdBvnvfi/81i348/BrfV1T6I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1603468138; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=oTHycSRmLA65HL97g9avNjqsTxBZTSW/qVBAreWcHcU=; b=ERMYH0j7QMg71765jTo7xbMocMP9fOjrW2B7pDmtwx43CS1o/4AlAMkJ2G+8jNxF5ppB5tI76J8Ef77hZAv7HrJBMZGH9aVkDIG5cFojBvzeHrkuykRL6Da9We1fLAiv2VsOrO9/94NmFzfXl0IZIg4GWV4n8W6bXOsswaub+8Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1603468138810841.542763170957; Fri, 23 Oct 2020 08:48:58 -0700 (PDT) Received: from localhost ([::1]:48104 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kVzJJ-00067n-LX for importer@patchew.org; Fri, 23 Oct 2020 11:48:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58822) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kVyun-0006bm-8o for qemu-devel@nongnu.org; Fri, 23 Oct 2020 11:23:37 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:24391) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kVyul-0000qw-GR for qemu-devel@nongnu.org; Fri, 23 Oct 2020 11:23:36 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-197-_JWxZGgWPhK6CCVsUvVL-w-1; Fri, 23 Oct 2020 11:23:30 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2BDF15F9F6; Fri, 23 Oct 2020 15:23:29 +0000 (UTC) Received: from localhost (ovpn-114-178.ams2.redhat.com [10.36.114.178]) by smtp.corp.redhat.com (Postfix) with ESMTP id 661C46EF6B; Fri, 23 Oct 2020 15:22:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1603466614; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oTHycSRmLA65HL97g9avNjqsTxBZTSW/qVBAreWcHcU=; b=Rjd2zgGzK0tWBxXuBHtewyH2Pv4y8nAnDjAp4uwAWT2uhbAfqmVAgJftGDMlpGNhzyU9lS nVW8ZE0nhiZsu8cut40z50qHIvk0awBGBt56HQcyoe7PXm31L+k6cBn/QVMEmaaJTzBjC2 VfDxkU3E/o0YNRea84CL90Nk40z6QUY= X-MC-Unique: _JWxZGgWPhK6CCVsUvVL-w-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org, Peter Maydell Subject: [PULL v3 13/28] util/vhost-user-server: fix memory leak in vu_message_read() Date: Fri, 23 Oct 2020 16:21:32 +0100 Message-Id: <20201023152147.1016281-14-stefanha@redhat.com> In-Reply-To: <20201023152147.1016281-1-stefanha@redhat.com> References: <20201023152147.1016281-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=stefanha@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/10/23 01:44:00 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -3 X-Spam_score: -0.4 X-Spam_bar: / X-Spam_report: (-0.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MIME_BASE64_TEXT=1.741, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Fam Zheng , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Eduardo Habkost , qemu-block@nongnu.org, "Dr. David Alan Gilbert" , Coiby Xu , Markus Armbruster , Stefan Hajnoczi , Paolo Bonzini , Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" fds[] is leaked when qio_channel_readv_full() fails. Use vmsg->fds[] instead of keeping a local fds[] array. Then we can reuse goto fail to clean up fds. vmsg->fd_num must be zeroed before the loop to make this safe. Signed-off-by: Stefan Hajnoczi Message-id: 20200924151549.913737-8-stefanha@redhat.com Signed-off-by: Stefan Hajnoczi --- util/vhost-user-server.c | 50 ++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/util/vhost-user-server.c b/util/vhost-user-server.c index 73a1667b54..a7b7a9897f 100644 --- a/util/vhost-user-server.c +++ b/util/vhost-user-server.c @@ -100,21 +100,11 @@ vu_message_read(VuDev *vu_dev, int conn_fd, VhostUser= Msg *vmsg) }; int rc, read_bytes =3D 0; Error *local_err =3D NULL; - /* - * Store fds/nfds returned from qio_channel_readv_full into - * temporary variables. - * - * VhostUserMsg is a packed structure, gcc will complain about passing - * pointer to a packed structure member if we pass &VhostUserMsg.fd_num - * and &VhostUserMsg.fds directly when calling qio_channel_readv_full, - * thus two temporary variables nfds and fds are used here. - */ - size_t nfds =3D 0, nfds_t =3D 0; const size_t max_fds =3D G_N_ELEMENTS(vmsg->fds); - int *fds_t =3D NULL; VuServer *server =3D container_of(vu_dev, VuServer, vu_dev); QIOChannel *ioc =3D server->ioc; =20 + vmsg->fd_num =3D 0; if (!ioc) { error_report_err(local_err); goto fail; @@ -122,41 +112,47 @@ vu_message_read(VuDev *vu_dev, int conn_fd, VhostUser= Msg *vmsg) =20 assert(qemu_in_coroutine()); do { + size_t nfds =3D 0; + int *fds =3D NULL; + /* * qio_channel_readv_full may have short reads, keeping calling it * until getting VHOST_USER_HDR_SIZE or 0 bytes in total */ - rc =3D qio_channel_readv_full(ioc, &iov, 1, &fds_t, &nfds_t, &loca= l_err); + rc =3D qio_channel_readv_full(ioc, &iov, 1, &fds, &nfds, &local_er= r); if (rc < 0) { if (rc =3D=3D QIO_CHANNEL_ERR_BLOCK) { + assert(local_err =3D=3D NULL); qio_channel_yield(ioc, G_IO_IN); continue; } else { error_report_err(local_err); - return false; + goto fail; } } - read_bytes +=3D rc; - if (nfds_t > 0) { - if (nfds + nfds_t > max_fds) { + + if (nfds > 0) { + if (vmsg->fd_num + nfds > max_fds) { error_report("A maximum of %zu fds are allowed, " "however got %zu fds now", - max_fds, nfds + nfds_t); + max_fds, vmsg->fd_num + nfds); + g_free(fds); goto fail; } - memcpy(vmsg->fds + nfds, fds_t, - nfds_t *sizeof(vmsg->fds[0])); - nfds +=3D nfds_t; - g_free(fds_t); + memcpy(vmsg->fds + vmsg->fd_num, fds, nfds * sizeof(vmsg->fds[= 0])); + vmsg->fd_num +=3D nfds; + g_free(fds); } - if (read_bytes =3D=3D VHOST_USER_HDR_SIZE || rc =3D=3D 0) { - break; + + if (rc =3D=3D 0) { /* socket closed */ + goto fail; } - iov.iov_base =3D (char *)vmsg + read_bytes; - iov.iov_len =3D VHOST_USER_HDR_SIZE - read_bytes; - } while (true); =20 - vmsg->fd_num =3D nfds; + iov.iov_base +=3D rc; + iov.iov_len -=3D rc; + read_bytes +=3D rc; + } while (read_bytes !=3D VHOST_USER_HDR_SIZE); + /* qio_channel_readv_full will make socket fds blocking, unblock them = */ vmsg_unblock_fds(vmsg); if (vmsg->size > sizeof(vmsg->payload)) { --=20 2.26.2