From nobody Mon Sep 16 19:08:29 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 1721731320713666.3591540350163; Tue, 23 Jul 2024 03:42:00 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 7D67AA77; Tue, 23 Jul 2024 06:41:59 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 7457AA99; Tue, 23 Jul 2024 06:41:41 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id B447FA05; Tue, 23 Jul 2024 06:41:38 -0400 (EDT) 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 3A98B9EE for ; Tue, 23 Jul 2024 06:41:38 -0400 (EDT) Received: from mx-prod-mc-02.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-370-nnkkwhMGN6ucWwH0hKYaIw-1; Tue, 23 Jul 2024 06:41:35 -0400 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id F02CE1955D4A for ; Tue, 23 Jul 2024 10:41:34 +0000 (UTC) Received: from maggie.brq.redhat.com (unknown [10.43.3.102]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 2418C1955F40 for ; Tue, 23 Jul 2024 10:41:33 +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_H4,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=1721731297; 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=2UmjJTPWLTYjQpsdds0Ca6SDnMgKPSLHVTcgFayEyO0=; b=UNpCxN7n6dOoMlR+9tXFEHfwLKdBh1gJL7nvBthFTF2m8nAOJmqP+dbcXPRtZghth6fdDH 9ZC1IaUXlK1/R4Zwjg3PzrzQ2TaF/vKWT1dbLIPnsLAu44Ou+H2kD4MqvW7RlebxF0usJF ZwgnzRXfcN8rNKMbLD79HgJCYwPxzPg= X-MC-Unique: nnkkwhMGN6ucWwH0hKYaIw-1 From: Michal Privoznik To: devel@lists.libvirt.org Subject: [PATCH] virt-host-validate: Allow longer list of CPU flags Date: Tue, 23 Jul 2024 12:41:31 +0200 Message-ID: <2b6c754248f8e315529d3b401f2ae19f9b44e22f.1721731291.git.mprivozn@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: LK74PN7JIDVKY5MZC4664RZTMOG7F5AR X-Message-ID-Hash: LK74PN7JIDVKY5MZC4664RZTMOG7F5AR 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: 1721731321901116600 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 --- tools/virt-host-validate-common.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/tools/virt-host-validate-common.c b/tools/virt-host-validate-c= ommon.c index 591143c24d..e5fa6606d2 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,12 +127,6 @@ 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'; - /* Skip to the separator */ if (!(start =3D strchr(line, ':'))) continue; @@ -153,7 +145,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