From nobody Fri May 3 04:31:21 2024 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1622020599; cv=none; d=zohomail.com; s=zohoarc; b=FLNe1qyhemF5AWJkiHabaHNcnekoWKILu92wOMJ2cj/H9kDS3kyRUXHh1Quf8wNw3a0yT5vE33+2Juenkqx6EEPNPcIatBPCHjOqrV5EUwrvxQzMa8YeQMDjzIL2JeM10gldHS57kPU5U8lDOux2SEGNXwez3k/sWSdahQtvhis= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1622020599; 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=Y9hJazNPjnNGXKvVssgVxrNnZtWYBrekYXBFmnw2ric=; b=DVmc5RJRDTUc5FkibJqP2Ac29hilMUWyVQP2vIACSmldNi/eR74Iyqvx7PrljLTPYzoIZem8TC0oUzpXC8tMqtYrQmGX+10sf9W+52hqddZ2jpJn7f560UYMbOQJKelhs0JHZxYzYsHHJJlw4yF2wKbgyWNWKrV4Q5a0dEmwRUw= 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+importer2=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 162202059994278.9582835524509; Wed, 26 May 2021 02:16:39 -0700 (PDT) Received: from localhost ([::1]:44184 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1llpeY-0006Qd-RI for importer2@patchew.org; Wed, 26 May 2021 05:16:38 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55526) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llpbF-0001q5-Aa for qemu-devel@nongnu.org; Wed, 26 May 2021 05:13:13 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:42429) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llpbC-0001B4-Vn for qemu-devel@nongnu.org; Wed, 26 May 2021 05:13:13 -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-124-L5_bsQrHMB6H8EHpff5DQw-1; Wed, 26 May 2021 05:13:05 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id DB9FEFC8D; Wed, 26 May 2021 09:13:04 +0000 (UTC) Received: from localhost (ovpn-115-80.ams2.redhat.com [10.36.115.80]) by smtp.corp.redhat.com (Postfix) with ESMTP id B6FCA61094; Wed, 26 May 2021 09:12:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622020390; 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=Y9hJazNPjnNGXKvVssgVxrNnZtWYBrekYXBFmnw2ric=; b=WTd+03ppv6n2Mn1FoNeJumJzNOlZnvohTzLbZhWQygzOB0A+7wgv0l81/xl1Agw0/SH9WG NauJPu5i0vPQMyrUBkDA+0AX5/UOfASNBGNUo0p4jkWdL0RWdQZaaq8WYQTKYSWR51rv9s 0XskZIkan136bQdMSnUh1sePO/ZdC0c= X-MC-Unique: L5_bsQrHMB6H8EHpff5DQw-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [PATCH 1/3] vhost-user-blk-test: fix Coverity open(2) false positives Date: Wed, 26 May 2021 10:12:46 +0100 Message-Id: <20210526091248.434459-2-stefanha@redhat.com> In-Reply-To: <20210526091248.434459-1-stefanha@redhat.com> References: <20210526091248.434459-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -13 X-Spam_score: -1.4 X-Spam_bar: - X-Spam_report: (-1.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.371, 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_LOW=-0.7, 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: Laurent Vivier , Kevin Wolf , Thomas Huth , qemu-block@nongnu.org, Coiby Xu , Gerd Hoffmann , Stefan Hajnoczi , Paolo Bonzini Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Coverity checks that the file descriptor return value of open(2) is checked and used. Normally this is helpful in identifying real bugs but vhost-user-blk-test opens /dev/null as stdin and stdout after fork. In this case we don't need to look at the return value because these open(2) calls cannot fail in any reasonable environment. We already know their return values ahead of time since we closed stdin and stdout previously. open(2) is guaranteed to pick the lowest available fd number. Silence Coverity by introducing code that checks what we already know to be true. ** CID 1453270: Resource leaks (RESOURCE_LEAK) /qemu/tests/qtest/vhost-user-blk-test.c: 920 in start_vhost_user_blk() ___________________________________________________________________________= _____________________________ *** CID 1453270: Resource leaks (RESOURCE_LEAK) /qemu/tests/qtest/vhost-user-blk-test.c: 920 in start_vhost_user_blk() 914 * Close standard file descriptors so tap-driver.pl pipe de= tects when 915 * our parent terminates. 916 */ 917 close(0); 918 close(1); 919 open("/dev/null", O_RDONLY); >>> CID 1453270: Resource leaks (RESOURCE_LEAK) >>> Ignoring handle opened by "open("/dev/null", 1)" leaks it. 920 open("/dev/null", O_WRONLY); 921 922 execlp("/bin/sh", "sh", "-c", storage_daemon_command->str, = NULL); 923 exit(1); 924 } 925 g_string_free(storage_daemon_command, true); ** CID 1453269: Error handling issues (NEGATIVE_RETURNS) /qemu/tests/qtest/vhost-user-blk-test.c: 829 in create_listen_socket() ___________________________________________________________________________= _____________________________ *** CID 1453269: Error handling issues (NEGATIVE_RETURNS) /qemu/tests/qtest/vhost-user-blk-test.c: 829 in create_listen_socket() 823 char *path; 824 825 /* No race because our pid makes the path unique */ 826 path =3D g_strdup_printf("/tmp/qtest-%d-sock.XXXXXX", getpid()); 827 tmp_fd =3D mkstemp(path); 828 g_assert_cmpint(tmp_fd, >=3D, 0); >>> CID 1453269: Error handling issues (NEGATIVE_RETURNS) >>> "tmp_fd" is passed to a parameter that cannot be negative. 829 close(tmp_fd); 830 unlink(path); 831 832 *fd =3D qtest_socket_server(path); 833 g_test_queue_destroy(destroy_file, path); 834 return path; ** CID 1453268: (CHECKED_RETURN) /qemu/tests/qtest/vhost-user-blk-test.c: 920 in start_vhost_user_blk() /qemu/tests/qtest/vhost-user-blk-test.c: 919 in start_vhost_user_blk() ___________________________________________________________________________= _____________________________ *** CID 1453268: (CHECKED_RETURN) /qemu/tests/qtest/vhost-user-blk-test.c: 920 in start_vhost_user_blk() 914 * Close standard file descriptors so tap-driver.pl pipe de= tects when 915 * our parent terminates. 916 */ 917 close(0); 918 close(1); 919 open("/dev/null", O_RDONLY); >>> CID 1453268: (CHECKED_RETURN) >>> Calling "open("/dev/null", 1)" without checking return value. This = library function may fail and return an error code. [Note: The source code = implementation of the function has been overridden by a builtin model.] 920 open("/dev/null", O_WRONLY); 921 922 execlp("/bin/sh", "sh", "-c", storage_daemon_command->str, = NULL); 923 exit(1); 924 } 925 g_string_free(storage_daemon_command, true); /qemu/tests/qtest/vhost-user-blk-test.c: 919 in start_vhost_user_blk() 913 /* 914 * Close standard file descriptors so tap-driver.pl pipe de= tects when 915 * our parent terminates. 916 */ 917 close(0); 918 close(1); >>> CID 1453268: (CHECKED_RETURN) >>> Calling "open("/dev/null", 0)" without checking return value. This = library function may fail and return an error code. [Note: The source code = implementation of the function has been overridden by a builtin model.] 919 open("/dev/null", O_RDONLY); 920 open("/dev/null", O_WRONLY); 921 922 execlp("/bin/sh", "sh", "-c", storage_daemon_command->str, = NULL); 923 exit(1); 924 } Signed-off-by: Stefan Hajnoczi --- tests/qtest/vhost-user-blk-test.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/qtest/vhost-user-blk-test.c b/tests/qtest/vhost-user-blk= -test.c index 8796c74ca4..581e283a03 100644 --- a/tests/qtest/vhost-user-blk-test.c +++ b/tests/qtest/vhost-user-blk-test.c @@ -910,14 +910,18 @@ static void start_vhost_user_blk(GString *cmd_line, i= nt vus_instances, storage_daemon_command->str); pid_t pid =3D fork(); if (pid =3D=3D 0) { + int fd; + /* * Close standard file descriptors so tap-driver.pl pipe detects w= hen * our parent terminates. */ close(0); + fd =3D open("/dev/null", O_RDONLY); + g_assert_cmpint(fd, =3D=3D, 0); close(1); - open("/dev/null", O_RDONLY); - open("/dev/null", O_WRONLY); + fd =3D open("/dev/null", O_WRONLY); + assert(fd =3D=3D 1); =20 execlp("/bin/sh", "sh", "-c", storage_daemon_command->str, NULL); exit(1); --=20 2.31.1 From nobody Fri May 3 04:31:21 2024 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1622020488; cv=none; d=zohomail.com; s=zohoarc; b=Rr1y55PeBl9ZqJaY33CRSdkNfQMmj1SqgrHdXL3xpJSlOXsvEEA0+a+vWliGUveQcUU2SfaQcFVaBNzoI4B3EfsRyGbnlWkXVKI58Uj+I3m0zv2NIDMG0pztAd3hu8nAJSessmyiJJOa9hfuGsHBtcjQbuV4hQ6VFE9WHvAizeU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1622020488; 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=OjzrBE27jlzhMuAkjUiyZ08J6x+y8wyOEtbfqNd9XZ4=; b=CTMeWvK0u+qGMb8liG3vMfUmVSEPV3gDkUMFz1tUVIzhj5UwMsE9TKHbS+A+1J2jNQX5bmQzqWajiC4hGetAQ9HfwosSlYZ2umq0/EfSyuKkvDi2552wijK7vpqEeJq3pbRVOl2Kubg2ULShUfS9gnpQ1qIRfbXdhVw+QvowkuM= 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+importer2=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 1622020488552101.28972005282355; Wed, 26 May 2021 02:14:48 -0700 (PDT) Received: from localhost ([::1]:39782 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1llpcj-0003Na-Np for importer2@patchew.org; Wed, 26 May 2021 05:14:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55518) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llpbF-0001pW-1L for qemu-devel@nongnu.org; Wed, 26 May 2021 05:13:13 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:36550) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llpbB-0001AH-T5 for qemu-devel@nongnu.org; Wed, 26 May 2021 05:13:12 -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-555-Cxo4bY3VMzOUuwqnrHiLJw-1; Wed, 26 May 2021 05:13:07 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 604A7801B13; Wed, 26 May 2021 09:13:06 +0000 (UTC) Received: from localhost (ovpn-115-80.ams2.redhat.com [10.36.115.80]) by smtp.corp.redhat.com (Postfix) with ESMTP id 046E35D9D3; Wed, 26 May 2021 09:13:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622020389; 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=OjzrBE27jlzhMuAkjUiyZ08J6x+y8wyOEtbfqNd9XZ4=; b=ZwOnPL24s48teRPVYhiGnlcceu1lAP31sOZJXXdO9nYklEXDCleLK7YWmVG5gW9lsxcno5 jbfA7QKGtsiSa5Ys3DksynQyd5AMrBuvPn+uohRTlnnNBo2R2ABmOGUM6Vw42dKwGQ3dFn P2A0YY9Ovn5gyAJQ2JQlNZI5vqde+VA= X-MC-Unique: Cxo4bY3VMzOUuwqnrHiLJw-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [PATCH 2/3] vhost-user-blk-test: fix Coverity mkstemp(2) umask warning Date: Wed, 26 May 2021 10:12:47 +0100 Message-Id: <20210526091248.434459-3-stefanha@redhat.com> In-Reply-To: <20210526091248.434459-1-stefanha@redhat.com> References: <20210526091248.434459-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 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+importer2=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-Spam_score_int: -13 X-Spam_score: -1.4 X-Spam_bar: - X-Spam_report: (-1.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.371, 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_LOW=-0.7, 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: Laurent Vivier , Kevin Wolf , Thomas Huth , qemu-block@nongnu.org, Coiby Xu , Gerd Hoffmann , Stefan Hajnoczi , Paolo Bonzini Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" The Linux man page for mkstemp(3) states: In glibc versions 2.06 and earlier, the file is created with permissions 0666, that is, read and write for all users. This old behavior may be a security risk, especially since other UNIX flavors use 0600, and somebody might overlook this detail when porting programs. POSIX.1-2008 adds a requirement that the file be created with mode 0600. More generally, the POSIX specification of mkstemp() does not say anything about file modes, so the application should make sure its file mode creation mask (see umask(2)) is set appropriately before calling mkstemp() (and mkostemp()). glibc 2.0.6 was released in 1997 and POSIX caught up in 2008. macOS and FreeBSD also use POSIX-compliant 0600 permissions. At this point the Coverity warning seems archaic and no longer useful, but go ahead and silence it. *** CID 1453267: Security best practices violations (SECURE_TEMP) /qemu/tests/qtest/vhost-user-blk-test.c: 827 in create_listen_socket() 821 { 822 int tmp_fd; 823 char *path; 824 825 /* No race because our pid makes the path unique */ 826 path =3D g_strdup_printf("/tmp/qtest-%d-sock.XXXXXX", getpid()); >>> CID 1453267: Security best practices violations (SECURE_TEMP) >>> Calling "mkstemp" without securely setting umask first. 827 tmp_fd =3D mkstemp(path); 828 g_assert_cmpint(tmp_fd, >=3D, 0); 829 close(tmp_fd); 830 unlink(path); 831 832 *fd =3D qtest_socket_server(path); Signed-off-by: Stefan Hajnoczi --- tests/qtest/vhost-user-blk-test.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/qtest/vhost-user-blk-test.c b/tests/qtest/vhost-user-blk= -test.c index 581e283a03..412e010db8 100644 --- a/tests/qtest/vhost-user-blk-test.c +++ b/tests/qtest/vhost-user-blk-test.c @@ -803,11 +803,16 @@ static void destroy_file(void *path) static char *drive_create(void) { int fd, ret; + mode_t old_umask; /** vhost-user-blk won't recognize drive located in /tmp */ char *t_path =3D g_strdup("qtest.XXXXXX"); =20 /** Create a temporary raw image */ + old_umask =3D umask(S_IXUSR | + S_IRGRP | S_IWGRP | S_IXGRP | + S_IROTH | S_IWOTH | S_IXOTH); fd =3D mkstemp(t_path); + umask(old_umask); g_assert_cmpint(fd, >=3D, 0); ret =3D ftruncate(fd, TEST_IMAGE_SIZE); g_assert_cmpint(ret, =3D=3D, 0); @@ -821,10 +826,15 @@ static char *create_listen_socket(int *fd) { int tmp_fd; char *path; + mode_t old_umask; =20 /* No race because our pid makes the path unique */ path =3D g_strdup_printf("/tmp/qtest-%d-sock.XXXXXX", getpid()); + old_umask =3D umask(S_IXUSR | + S_IRGRP | S_IWGRP | S_IXGRP | + S_IROTH | S_IWOTH | S_IXOTH); tmp_fd =3D mkstemp(path); + umask(old_umask); g_assert_cmpint(tmp_fd, >=3D, 0); close(tmp_fd); unlink(path); --=20 2.31.1 From nobody Fri May 3 04:31:21 2024 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1622020595; cv=none; d=zohomail.com; s=zohoarc; b=eKe/RIMG3hs3AgdY7JvBllsrPw1xe5fQRYMCNNrZ6fHp1JbOhQP13W00904Ra1Hq9JPyimuTKHJUV/aTzHWmr6chhkMoooDzL31P3hAh87YxiVtRUf19LEW8nqCpU7qPMtIZP5REtJAu+QSUApFI1vOIJKymcIWGrXgIuZz5a0A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1622020595; 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=omTh+2VqrlWG+mA8A8bpUVVby68xoQ90QJPFIO0Jcus=; b=GEZbjrCZcezzJp1v9REizFE+0S3BgvGXVOWO5DL4KC2g8Ab+wlXEBlnUS6kux4LjGWlqW7hZdBKGwZ1owg47kYhr2WiphBWlq9inipBUCnya8fNWcP9r1KXdjYesx9JoRdHhqON5rGlyI+Rclq/xR2FyQH7B0VSL6MiTp8u17Es= 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+importer2=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 1622020595744410.85771575574586; Wed, 26 May 2021 02:16:35 -0700 (PDT) Received: from localhost ([::1]:43944 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1llpeU-0006Gu-NI for importer2@patchew.org; Wed, 26 May 2021 05:16:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55576) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llpbR-00022x-Mf for qemu-devel@nongnu.org; Wed, 26 May 2021 05:13:27 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:53166) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llpbL-0001Gu-M2 for qemu-devel@nongnu.org; Wed, 26 May 2021 05:13:23 -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-140-reEX55CANoKMdtYsXMjNOQ-1; Wed, 26 May 2021 05:13:15 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0351D100945F; Wed, 26 May 2021 09:13:15 +0000 (UTC) Received: from localhost (ovpn-115-80.ams2.redhat.com [10.36.115.80]) by smtp.corp.redhat.com (Postfix) with ESMTP id 81C4F5D75A; Wed, 26 May 2021 09:13:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622020399; 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=omTh+2VqrlWG+mA8A8bpUVVby68xoQ90QJPFIO0Jcus=; b=IDX1LQUXMxihactxa9DUry996n6BTRoZPXUcN2+RYu8+6FoWPilodG4vypCwYE1usbtLKk aVGCVL70MfnxgW1YCEaEc+kKNhu3vHwPLcDY8qFyxTvxEUMr4yiP9WXEyCP8rJze35nPXb 4/xlp/Qcex/YMOARHPwNQn8fsy094zs= X-MC-Unique: reEX55CANoKMdtYsXMjNOQ-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [PATCH 3/3] ui/vdagent: fix clipboard info memory leak in error path Date: Wed, 26 May 2021 10:12:48 +0100 Message-Id: <20210526091248.434459-4-stefanha@redhat.com> In-Reply-To: <20210526091248.434459-1-stefanha@redhat.com> References: <20210526091248.434459-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 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+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -13 X-Spam_score: -1.4 X-Spam_bar: - X-Spam_report: (-1.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.371, 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_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: Laurent Vivier , Kevin Wolf , Thomas Huth , qemu-block@nongnu.org, Coiby Xu , Gerd Hoffmann , Stefan Hajnoczi , Paolo Bonzini Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" If the size of a VD_AGENT_CLIPBOARD_GRAB message is invalid we leak info when returning early. Thanks to Coverity for spotting this: *** CID 1453266: Resource leaks (RESOURCE_LEAK) /qemu/ui/vdagent.c: 465 in vdagent_chr_recv_clipboard() 459 info =3D qemu_clipboard_info_new(&vd->cbpeer, s); 460 if (size > sizeof(uint32_t) * 10) { 461 /* 462 * spice has 6 types as of 2021. Limiting to 10 entries 463 * so we we have some wiggle room. 464 */ >>> CID 1453266: Resource leaks (RESOURCE_LEAK) >>> Variable "info" going out of scope leaks the storage it points to. 465 return; 466 } 467 while (size >=3D sizeof(uint32_t)) { 468 trace_vdagent_cb_grab_type(GET_NAME(type_name, *(uint32= _t *)data)); 469 switch (*(uint32_t *)data) { 470 case VD_AGENT_CLIPBOARD_UTF8_TEXT: Fixes: f0349f4d8947ad32d0fa4678cbf5dbb356fcbda1 ("ui/vdagent: add clipboard= support") Cc: Gerd Hoffmann Signed-off-by: Stefan Hajnoczi Reviewed-by: Gerd Hoffmann --- ui/vdagent.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/vdagent.c b/ui/vdagent.c index a253a8fe63..8fc54d330e 100644 --- a/ui/vdagent.c +++ b/ui/vdagent.c @@ -456,7 +456,6 @@ static void vdagent_chr_recv_clipboard(VDAgentChardev *= vd, VDAgentMessage *msg) switch (msg->type) { case VD_AGENT_CLIPBOARD_GRAB: trace_vdagent_cb_grab_selection(GET_NAME(sel_name, s)); - info =3D qemu_clipboard_info_new(&vd->cbpeer, s); if (size > sizeof(uint32_t) * 10) { /* * spice has 6 types as of 2021. Limiting to 10 entries @@ -464,6 +463,7 @@ static void vdagent_chr_recv_clipboard(VDAgentChardev *= vd, VDAgentMessage *msg) */ return; } + info =3D qemu_clipboard_info_new(&vd->cbpeer, s); while (size >=3D sizeof(uint32_t)) { trace_vdagent_cb_grab_type(GET_NAME(type_name, *(uint32_t *)da= ta)); switch (*(uint32_t *)data) { --=20 2.31.1