From nobody Mon Sep 16 20:09:18 2024 Delivered-To: importer@patchew.org Received-SPF: none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; spf=none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1706637780550105.22385221855382; Tue, 30 Jan 2024 10:03:00 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id 84E451DA4; Tue, 30 Jan 2024 13:02:59 -0500 (EST) Received: from lists.libvirt.org.85.43.8.in-addr.arpa (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id A4D7A1DDA; Tue, 30 Jan 2024 12:12:43 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id AC33D1CA5; Tue, 30 Jan 2024 12:10:44 -0500 (EST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 4118F1D37 for ; Tue, 30 Jan 2024 12:08:45 -0500 (EST) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-638-0-uX61d4MaOE3eNhJsOv-Q-1; Tue, 30 Jan 2024 12:08:42 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 40A95185A78B for ; Tue, 30 Jan 2024 17:08:42 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.45.242.16]) by smtp.corp.redhat.com (Postfix) with ESMTP id A7F3E2166B31 for ; Tue, 30 Jan 2024 17:08:41 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.4 X-MC-Unique: 0-uX61d4MaOE3eNhJsOv-Q-1 From: Peter Krempa To: devel@lists.libvirt.org Subject: [PATCH 30/31] virPCIVPDParseVPDLargeResourceFields: Report proper errors Date: Tue, 30 Jan 2024 18:08:08 +0100 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Message-ID-Hash: TB4MCIMDQDCN7XXDTPFRZZ5QWOCLT7HI X-Message-ID-Hash: TB4MCIMDQDCN7XXDTPFRZZ5QWOCLT7HI X-MailFrom: pkrempa@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1706637783131100001 The code abused 'VIR_INFO' as an attempt at error reporting. Rework the code to return the usual 0/-1 and raise proper errors. Signed-off-by: Peter Krempa --- src/util/virpcivpd.c | 67 +++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/src/util/virpcivpd.c b/src/util/virpcivpd.c index be19f7b747..4a440c2aea 100644 --- a/src/util/virpcivpd.c +++ b/src/util/virpcivpd.c @@ -404,12 +404,15 @@ virPCIVPDReadVPDBytes(int vpdFileFd, * @csum: A pointer to a 1-byte checksum. * @res: A pointer to virPCIVPDResource. * - * Returns: a pointer to a VPDResource which needs to be freed by the call= er or - * NULL if getting it failed for some reason. + * Returns 0 if the field was parsed sucessfully; -1 on error */ -static bool -virPCIVPDParseVPDLargeResourceFields(int vpdFileFd, uint16_t resPos, uint1= 6_t resDataLen, - bool readOnly, uint8_t *csum, virPCIV= PDResource *res) +static int +virPCIVPDParseVPDLargeResourceFields(int vpdFileFd, + uint16_t resPos, + uint16_t resDataLen, + bool readOnly, + uint8_t *csum, + virPCIVPDResource *res) { /* A buffer of up to one resource record field size (plus a zero byte)= is needed. */ g_autofree uint8_t *buf =3D g_malloc0(PCI_VPD_MAX_FIELD_SIZE + 1); @@ -427,7 +430,7 @@ virPCIVPDParseVPDLargeResourceFields(int vpdFileFd, uin= t16_t resPos, uint16_t re /* Keyword resources consist of keywords (2 ASCII bytes per the sp= ec) and 1-byte length. */ if (virPCIVPDReadVPDBytes(vpdFileFd, buf, 3, fieldPos, csum) < 0) - return false; + return -1; fieldDataLen =3D buf[2]; /* Change the position to the field's data portion skipping the ke= yword and length bytes. */ @@ -444,8 +447,9 @@ virPCIVPDParseVPDLargeResourceFields(int vpdFileFd, uin= t16_t resPos, uint16_t re case VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_RDWR: if (readOnly) { - VIR_INFO("Unexpected RW keyword in the read-only secti= on."); - return false; + virReportError(VIR_ERR_OPERATION_FAILED, "%s", + _("failed to read the PCI VPD data: une= xpected RW keyword in read-only section")); + return -1; } bytesToRead =3D fieldDataLen; @@ -453,8 +457,9 @@ virPCIVPDParseVPDLargeResourceFields(int vpdFileFd, uin= t16_t resPos, uint16_t re case VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_RESVD: if (!readOnly) { - VIR_INFO("Unexpected RV keyword in the read-write sect= ion."); - return false; + virReportError(VIR_ERR_OPERATION_FAILED, "%s", + _("failed to read the PCI VPD data: une= xpected RV keyword in read-write section")); + return -1; } /* Only need one byte to be read and accounted towards * the checksum calculation. */ @@ -472,12 +477,13 @@ virPCIVPDParseVPDLargeResourceFields(int vpdFileFd, u= int16_t resPos, uint16_t re if (resPos + resDataLen < fieldPos + fieldDataLen) { /* In this case the field cannot simply be skipped since the p= osition of the * next field is determined based on the length of a previous = field. */ - VIR_INFO("A field data length violates the resource length bou= ndary."); - return false; + virReportError(VIR_ERR_OPERATION_FAILED, "%s", + _("failed to read the PCI VPD data: data field = length invalid")); + return -1; } if (virPCIVPDReadVPDBytes(vpdFileFd, buf, bytesToRead, fieldPos, c= sum) < 0) - return false; + return -1; /* Advance the position to the first byte of the next field. */ fieldPos +=3D fieldDataLen; @@ -492,7 +498,6 @@ virPCIVPDParseVPDLargeResourceFields(int vpdFileFd, uin= t16_t resPos, uint16_t re if (!virPCIVPDResourceIsValidTextValue(fieldValue)) { /* Skip fields with invalid values - this is safe assu= ming field length is * correctly specified. */ - VIR_DEBUG("A value for field %s contains invalid chara= cters", fieldKeyword); continue; } break; @@ -512,8 +517,9 @@ virPCIVPDParseVPDLargeResourceFields(int vpdFileFd, uin= t16_t resPos, uint16_t re case VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_RESVD: if (*csum) { /* All bytes up to and including the checksum byte sho= uld add up to 0. */ - VIR_INFO("Checksum validation has failed"); - return false; + virReportError(VIR_ERR_OPERATION_FAILED, "%s", + _("failed to read the PCI VPD data: inv= alid checksum")); + return -1; } hasChecksum =3D true; goto done; @@ -540,16 +546,18 @@ virPCIVPDParseVPDLargeResourceFields(int vpdFileFd, u= int16_t resPos, uint16_t re * they were not the last fields in the section. */ if ((fieldPos < resPos + resDataLen)) { /* unparsed data still present */ - VIR_DEBUG("PCI VPD data parsing failed"); - return false; + virReportError(VIR_ERR_OPERATION_FAILED, "%s", + _("failed to read the PCI VPD data: parsing ended p= rematurely")); + return -1; } if (readOnly && !hasChecksum) { - VIR_DEBUG("VPD-R does not contain the mandatory checksum"); - return false; + virReportError(VIR_ERR_OPERATION_FAILED, "%s", + _("failed to read the PCI VPD data: missing mandato= ry checksum")); + return -1; } - return true; + return 0; } @@ -606,7 +614,6 @@ virPCIVPDParse(int vpdFileFd) uint8_t csum =3D 0; uint8_t headerBuf[2]; - bool isWellFormed =3D false; uint16_t resPos =3D 0, resDataLen; uint8_t tag =3D 0; bool endResReached =3D false, hasReadOnly =3D false; @@ -659,20 +666,21 @@ virPCIVPDParse(int vpdFileFd) &csum, res) < 0) return NULL; - isWellFormed =3D true; break; /* Large resource type which is also a VPD-R: 0x80 | 0x10 = =3D=3D 0x90 */ case PCI_VPD_LARGE_RESOURCE_FLAG | PCI_VPD_READ_ONLY_LARGE_RES= OURCE_FLAG: - isWellFormed =3D virPCIVPDParseVPDLargeResourceFields(vpdF= ileFd, resPos, - resDat= aLen, true, &csum, res); + if (virPCIVPDParseVPDLargeResourceFields(vpdFileFd, resPos, + resDataLen, true,= &csum, res) < 0) + return NULL; /* Encountered the VPD-R tag. The resource record parsing = also validates * the presence of the required checksum in the RV field. = */ hasReadOnly =3D true; break; /* Large resource type which is also a VPD-W: 0x80 | 0x11 = =3D=3D 0x91 */ case PCI_VPD_LARGE_RESOURCE_FLAG | PCI_VPD_READ_WRITE_LARGE_RE= SOURCE_FLAG: - isWellFormed =3D virPCIVPDParseVPDLargeResourceFields(vpdF= ileFd, resPos, resDataLen, - false,= &csum, res); + if (virPCIVPDParseVPDLargeResourceFields(vpdFileFd, resPos= , resDataLen, + false, &csum, res= ) < 0) + return NULL; break; default: /* While we cannot parse unknown resource types, they can = still be skipped @@ -682,11 +690,6 @@ virPCIVPDParse(int vpdFileFd) continue; } - if (!isWellFormed) { - VIR_DEBUG("Encountered an invalid VPD"); - return NULL; - } - /* Continue processing other resource records. */ resPos +=3D resDataLen; } --=20 2.43.0 _______________________________________________ Devel mailing list -- devel@lists.libvirt.org To unsubscribe send an email to devel-leave@lists.libvirt.org