From nobody Sat May 4 00:00:57 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=huawei.com ARC-Seal: i=1; a=rsa-sha256; t=1616730205; cv=none; d=zohomail.com; s=zohoarc; b=Q4T4op/RN8MyQ5F2eTuIUhIvhW1MGBUaV0ok6RLt45nT6TfacmDZ/spBLked82N2AR1O7KpSV6XKkvrA/zVnd+BSKRx583q6x4/Tk8OUygGNandiK5tS/kxUnJA3GeN7/1vYr84WBC4932XGN8sqteI63Spp/UL0RrnUizY41V0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1616730205; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=1Hz/iMnmu3nEarZZKtmLfBhwMD+fQpsX7kLNS2dXy1g=; b=f3YkRkD+8lj6kp7PxEyLFhKH+Iwl5DraeoI5Dm+sPpKK9/Pcp9wkjui3/LAP9TXcuYMkcn/PgE9Li8EcR6VN6F3m/EYS8FnABfvy+gklEUOVdIPkcclTeW38xI/E38I/tFIzXMgUVpbdWUHJvWskOQ5EW/pbw3kMnioslQmdEvs= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1616730205501651.3205334448685; Thu, 25 Mar 2021 20:43:25 -0700 (PDT) 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-29-88QKJTckODKRstjoUl8xqg-1; Thu, 25 Mar 2021 23:43:22 -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 1DD0C1007468; Fri, 26 Mar 2021 03:43:15 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9756D2B88A; Fri, 26 Mar 2021 03:43:12 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 4A3A21809C81; Fri, 26 Mar 2021 03:43:08 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 12Q3eQTa005699 for ; Thu, 25 Mar 2021 23:40:26 -0400 Received: by smtp.corp.redhat.com (Postfix) id 00DFBF8978; Fri, 26 Mar 2021 03:40:26 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast04.extmail.prod.ext.rdu2.redhat.com [10.11.55.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id EFA56F89FC for ; Fri, 26 Mar 2021 03:40:23 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 708E3101A531 for ; Fri, 26 Mar 2021 03:40:23 +0000 (UTC) Received: from szxga06-in.huawei.com (szxga06-in.huawei.com [45.249.212.32]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-328-upwPE3BJMHCDzaMpRKuq6g-1; Thu, 25 Mar 2021 23:40:18 -0400 Received: from DGGEMS410-HUB.china.huawei.com (unknown [172.30.72.58]) by szxga06-in.huawei.com (SkyGuard) with ESMTP id 4F66f95cVyzkgRb; Fri, 26 Mar 2021 11:19:45 +0800 (CST) Received: from [10.174.187.80] (10.174.187.80) by DGGEMS410-HUB.china.huawei.com (10.3.19.210) with Microsoft SMTP Server id 14.3.498.0; Fri, 26 Mar 2021 11:21:16 +0800 X-MC-Unique: 88QKJTckODKRstjoUl8xqg-1 X-MC-Unique: upwPE3BJMHCDzaMpRKuq6g-1 From: wangjian Subject: [PATCH] serialize access pci_get_strings function with a mutex To: , , , , , , Message-ID: <44cd68af-6699-d4d0-0229-3edb80b33985@huawei.com> Date: Fri, 26 Mar 2021 11:21:16 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.9.0 MIME-Version: 1.0 X-Originating-IP: [10.174.187.80] X-CFilter-Loop: Reflected X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-loop: libvir-list@redhat.com Cc: libvir-list@redhat.com, eric.fangyi@huawei.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" serialize access pci_get_strings function with a mutex. nodedev-init thread: nodeStateInitializeEnumerate -> udevEnumerateDevices-> udevProcessDeviceListEntry -> udevAddOneDevice -> udevGetDeviceDetails-> udevProcessPCI -> udevTranslatePCIIds -> pci_get_strings -> (libpciaccess) find_device_name -> populate_vendor -> d =3D realloc( vend->devices, (vend->num_devices + 1), = * sizeof( struct pci_device_leaf ) ); vend->num_devices++; udev-event thread: udevEventHandleThread -> udevHandleOneDevice -> udevAddOneDevice-> udevGetDeviceDetails-> udevProcessPCI -> udevTranslatePCIIds -> pci_get_strings -> (libpciaccess) find_device_name -> populate_vendor -> d =3D realloc( vend->devices, (vend->num_devices + 1), * = sizeof( struct pci_device_leaf ) ); vend->num_devices++; Since the functions provided by libpciaccess are not thread-safe, when the udev-event and nodedev-init threads of libvirt call the pci_get_strings function provided by libpaciaccess at the same time. It will appear that for the same address, realloc a large space first, and then realloc a small space, which triggers the 'invalid next size' of r= ealloc, leading to the thread core. Signed-off-by: WangJian Reviewed-by: Michal Privoznik --- src/node_device/node_device_udev.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_devi= ce_udev.c index 3f28858..cc752ba 100644 --- a/src/node_device/node_device_udev.c +++ b/src/node_device/node_device_udev.c @@ -331,6 +331,7 @@ udevGenerateDeviceName(struct udev_device *device, return 0; } +static pthread_mutex_t g_pciaccess_mutex =3D PTHREAD_MUTEX_INITIALIZER; static int udevTranslatePCIIds(unsigned int vendor, @@ -350,11 +351,13 @@ udevTranslatePCIIds(unsigned int vendor, m.match_data =3D 0; /* pci_get_strings returns void */ + pthread_mutex_lock(&g_pciaccess_mutex); pci_get_strings(&m, &device_name, &vendor_name, NULL, NULL); + pthread_mutex_unlock(&g_pciaccess_mutex); *vendor_string =3D g_strdup(vendor_name); *product_string =3D g_strdup(device_name); --=20 2.23.0