From nobody Mon Sep 16 19:25:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) 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 1721735704634488.2594339033111; Tue, 23 Jul 2024 04:55:04 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 6FE54E91; Tue, 23 Jul 2024 07:55:03 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id E52D99ED; Tue, 23 Jul 2024 07:54:42 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id D8DEF9E4; Tue, 23 Jul 2024 07:54:39 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 5E6C79CC for ; Tue, 23 Jul 2024 07:54:39 -0400 (EDT) Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-351-e1hdVUBcOKSe7fW4lUiHYg-1; Tue, 23 Jul 2024 07:54:37 -0400 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8D224195608B for ; Tue, 23 Jul 2024 11:54:36 +0000 (UTC) Received: from maggie.brq.redhat.com (unknown [10.43.3.102]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id D3B9E19560AE for ; Tue, 23 Jul 2024 11:54:35 +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=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721735678; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=lcfetWR3ZQygqm9Ki1WAT3N5T9sh3/fftbpgpVVs2zY=; b=BHTnP+/bjSltKTn40LawhjmrLi9qm8HpFELTAXwUVcuYP5LM06Qd9HxYEWY+ytu9R/vqwL urSd5Kg4dZAhwjsQ8WDMWRrq003TCmyJQWNhXsYFenWKRH8N5W53EnfhEBuUJSRGnHXLsL xWZJAcC2daTpgDrmRvVIk9h/hDSHSa0= X-MC-Unique: e1hdVUBcOKSe7fW4lUiHYg-1 From: Michal Privoznik To: devel@lists.libvirt.org Subject: [PATCH v2] virt-host-validate: Allow longer list of CPU flags Date: Tue, 23 Jul 2024 13:54:33 +0200 Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: OQZCJOP5DQF7WVLFWUEVTRIMBVZOAKSU X-Message-ID-Hash: OQZCJOP5DQF7WVLFWUEVTRIMBVZOAKSU X-MailFrom: mprivozn@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: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1721735705696116600 Content-Type: text/plain; charset="utf-8"; x-default="true" On various occasions, virt-host-validate parses /proc/cpuinfo to learn about CPU flags (see virHostValidateGetCPUFlags()). It does so, by reading the file line by line until the line with CPU flags is reached. Then the line is split into individual flags (using space as a delimiter) and the list of flags is then iterated over. This works, except for cases when the line with CPU flags is too long. Problem is - the line is capped at 1024 bytes and on newer CPUs (and newer kernels), the line can be significantly longer. I've seen a line that's ~1200 characters long (with 164 flags reported). Switch to unbounded read from the file (getline()). Resolves: https://issues.redhat.com/browse/RHEL-39969 Signed-off-by: Michal Privoznik Reviewed-by: Jiri Denemark --- v2 of: https://lists.libvirt.org/archives/list/devel@lists.libvirt.org/thread/LK74= PN7JIDVKY5MZC4664RZTMOG7F5AR/ diff to v2: - Keep trimming of the optional newline tools/virt-host-validate-common.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/tools/virt-host-validate-common.c b/tools/virt-host-validate-c= ommon.c index 591143c24d..63cc3dbe7b 100644 --- a/tools/virt-host-validate-common.c +++ b/tools/virt-host-validate-common.c @@ -106,21 +106,19 @@ virBitmap *virHostValidateGetCPUFlags(void) { FILE *fp; virBitmap *flags =3D NULL; + g_autofree char *line =3D NULL; + size_t linelen =3D 0; =20 if (!(fp =3D fopen("/proc/cpuinfo", "r"))) return NULL; =20 flags =3D virBitmapNew(VIR_HOST_VALIDATE_CPU_FLAG_LAST); =20 - do { - char line[1024]; + while (getline(&line, &linelen, fp) > 0) { char *start; g_auto(GStrv) tokens =3D NULL; GStrv next; =20 - if (!fgets(line, sizeof(line), fp)) - break; - /* The line we're interested in is marked differently depending * on the architecture, so check possible prefixes */ if (!STRPREFIX(line, "flags") && @@ -129,11 +127,9 @@ virBitmap *virHostValidateGetCPUFlags(void) !STRPREFIX(line, "facilities")) continue; =20 - /* fgets() includes the trailing newline in the output buffer, - * so we need to clean that up ourselves. We can safely access - * line[strlen(line) - 1] because the checks above would cause - * us to skip empty strings */ - line[strlen(line) - 1] =3D '\0'; + /* getline() may include the trailing newline in the output + * buffer, so we need to clean that up ourselves. */ + virStringTrimOptionalNewline(line); =20 /* Skip to the separator */ if (!(start =3D strchr(line, ':'))) @@ -153,7 +149,7 @@ virBitmap *virHostValidateGetCPUFlags(void) if ((value =3D virHostValidateCPUFlagTypeFromString(*next)) >= =3D 0) ignore_value(virBitmapSetBit(flags, value)); } - } while (1); + } =20 VIR_FORCE_FCLOSE(fp); =20 --=20 2.44.2