From nobody Sun May 12 12:45:21 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=quarantine dis=quarantine) header.from=swemel.ru ARC-Seal: i=1; a=rsa-sha256; t=1694526959; cv=none; d=zohomail.com; s=zohoarc; b=kDyBAmKk7K3PhM3S6aQ5cY38tr7SHAiOZ1D7LShUEu5bA8EnipAsTK7geAoSMsc5WfA7eTckXvaWhduPoijYd1y4qFg60neZ3E/kAbhGHjrGZa8O5tSjO0e2hVsMxF5gzwaKPXvk5Lnllmh8l8PVun/Zg1TYDLS4Fg61W8AHnY8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1694526959; 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=R0GlmykkwKLL7sRWCk2MgBpBYkAbA6HyHO79WnCFQs4=; b=J03hAbP2eI2F0mHTNTuL1iT2gqZWorvo6PIVZOTX4rfliUkgDoQkCD6BwI4fd+D9r86Z6XDXtJNhdDFNQhf37DUd78ObJD5kBNvvrpNkFLbgR8I5pDGP6b9+RAwiT7C+ZNgywbHAbv6iYEqEaNTsg6SmJNzx4vheEYXQVcanEUU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=quarantine dis=quarantine) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 169452695914664.36888672249825; Tue, 12 Sep 2023 06:55:59 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-255-L34YEXBfM9a_FFXfLHUHyg-1; Tue, 12 Sep 2023 09:55:53 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 31CE1811C32; Tue, 12 Sep 2023 13:55:50 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 835327B62; Tue, 12 Sep 2023 13:55:47 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id EF3AA19465A8; Tue, 12 Sep 2023 13:55:46 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 80FDA194658D for ; Tue, 12 Sep 2023 12:57:31 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 6D9B040C6EBF; Tue, 12 Sep 2023 12:57:31 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast09.extmail.prod.ext.rdu2.redhat.com [10.11.55.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6659440C6EA8 for ; Tue, 12 Sep 2023 12:57:30 +0000 (UTC) Received: from us-smtp-inbound-delivery-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) (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 CC7C129AB413 for ; Tue, 12 Sep 2023 12:57:30 +0000 (UTC) Received: from mx.swemel.ru (mx.swemel.ru [95.143.211.150]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-9-SL-jHKuzMV2WV2gTnGYy_w-1; Tue, 12 Sep 2023 08:57:27 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694526958; h=from:from:sender:sender: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:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=R0GlmykkwKLL7sRWCk2MgBpBYkAbA6HyHO79WnCFQs4=; b=ZokqHFgAnSNPBtYDJ+j7Giu/cH93OpFNdd4GmUnUJMiWCzQtYj/9TglcOu6dHrD3cqHo00 Z5Dnj+uwcjDNvbnJKsfOTgD+dKn9MFAS48DmaUl7e6kuYWgdbBm4If9l/eQ+qGaoFHVg37 IHuf1JjEE13Jen48XDCIqe+vX+hArpA= X-MC-Unique: L34YEXBfM9a_FFXfLHUHyg-1 X-Original-To: libvir-list@listman.corp.redhat.com X-MC-Unique: SL-jHKuzMV2WV2gTnGYy_w-1 From: Dmitry Frolov To: libvir-list@redhat.com Subject: [PATCH v3] interface: fix udev_device_get_sysattr_value return value check Date: Tue, 12 Sep 2023 15:56:47 +0300 Message-Id: <20230912125646.156568-1-frolov@swemel.ru> MIME-Version: 1.0 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 3.1 on 10.11.54.2 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Dmitry Frolov Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: swemel.ru Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1694526961752100001 Content-Type: text/plain; charset="utf-8"; x-default="true" Reviewing the code I found that return value of function udev_device_get_sysattr_value() is dereferenced without a check. udev_device_get_sysattr_value() may return NULL by number of reasons. v2: VIR_DEBUG added, replaced STREQ(NULLSTR()) with STREQ_NULLABLE() v3: More checks added, to skip earlier. More verbose VIR_DEBUG. Signed-off-by: Dmitry Frolov Reviewed-by: Martin Kletzander --- src/interface/interface_backend_udev.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/interface/interface_backend_udev.c b/src/interface/interfa= ce_backend_udev.c index a0485ddd21..fb6799ed94 100644 --- a/src/interface/interface_backend_udev.c +++ b/src/interface/interface_backend_udev.c @@ -23,6 +23,7 @@ #include #include =20 +#include "virlog.h" #include "virerror.h" #include "virfile.h" #include "datatypes.h" @@ -40,6 +41,8 @@ =20 #define VIR_FROM_THIS VIR_FROM_INTERFACE =20 +VIR_LOG_INIT("interface.interface_backend_udev"); + struct udev_iface_driver { struct udev *udev; /* pid file FD, ensures two copies of the driver can't use the same ro= ot */ @@ -354,11 +357,20 @@ udevConnectListAllInterfaces(virConnectPtr conn, const char *macaddr; g_autoptr(virInterfaceDef) def =3D NULL; =20 - path =3D udev_list_entry_get_name(dev_entry); - dev =3D udev_device_new_from_syspath(udev, path); - name =3D udev_device_get_sysname(dev); + if (!(path =3D udev_list_entry_get_name(dev_entry))) { + VIR_DEBUG("Skipping interface, path =3D=3D NULL"); + continue; + } + if (!(dev =3D udev_device_new_from_syspath(udev, path))) { + VIR_DEBUG("Skipping interface '%s', dev =3D=3D NULL", path); + continue; + } + if (!(name =3D udev_device_get_sysname(dev))) { + VIR_DEBUG("Skipping interface '%s', name =3D=3D NULL", path); + continue; + } macaddr =3D udev_device_get_sysattr_value(dev, "address"); - status =3D STREQ(udev_device_get_sysattr_value(dev, "operstate"), = "up"); + status =3D STREQ_NULLABLE(udev_device_get_sysattr_value(dev, "oper= state"), "up"); =20 def =3D udevGetMinimalDefForDevice(dev); if (!virConnectListAllInterfacesCheckACL(conn, def)) { @@ -964,9 +976,9 @@ udevGetIfaceDef(struct udev *udev, const char *name) =20 /* MTU */ mtu_str =3D udev_device_get_sysattr_value(dev, "mtu"); - if (virStrToLong_ui(mtu_str, NULL, 10, &mtu) < 0) { + if (!mtu_str || virStrToLong_ui(mtu_str, NULL, 10, &mtu) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, - _("Could not parse MTU value '%1$s'"), mtu_str); + _("Could not parse MTU value '%1$s'"), NULLSTR(mtu_str)); goto error; } ifacedef->mtu =3D mtu; @@ -1089,7 +1101,7 @@ udevInterfaceIsActive(virInterfacePtr ifinfo) goto cleanup; =20 /* Check if it's active or not */ - status =3D STREQ(udev_device_get_sysattr_value(dev, "operstate"), "up"= ); + status =3D STREQ_NULLABLE(udev_device_get_sysattr_value(dev, "operstat= e"), "up"); =20 udev_device_unref(dev); =20 --=20 2.34.1