From nobody Sun Feb 8 18:28:28 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1491480703220146.14043609163093; Thu, 6 Apr 2017 05:11:43 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9E9E3624D7; Thu, 6 Apr 2017 12:11:41 +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 7253A9A57A; Thu, 6 Apr 2017 12:11:41 +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 0193818523C8; Thu, 6 Apr 2017 12:11:40 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v36CAvCF002289 for ; Thu, 6 Apr 2017 08:10:57 -0400 Received: by smtp.corp.redhat.com (Postfix) id C89C8171D0; Thu, 6 Apr 2017 12:10:57 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-19.phx2.redhat.com [10.3.116.19]) by smtp.corp.redhat.com (Postfix) with ESMTP id 88DB31719F for ; Thu, 6 Apr 2017 12:10:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 9E9E3624D7 Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 9E9E3624D7 From: John Ferlan To: libvir-list@redhat.com Date: Thu, 6 Apr 2017 08:10:48 -0400 Message-Id: <20170406121052.5815-5-jferlan@redhat.com> In-Reply-To: <20170406121052.5815-1-jferlan@redhat.com> References: <20170406121052.5815-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [RFC PATCH 4/8] util: Add magic_marker for all virObjects 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: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Thu, 06 Apr 2017 12:11:42 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The virObject logic "assumes" that whatever is passed to its API's would be some sort of virObjectPtr; however, if it is not then some really bad things can happen. So far there's been only virObject{Ref|Unref}, virObject{Lock|Unlock}, and virObjectIsClass and the virObject and virObjectLockable class consumers have been well behaved and code well tested. Soon there will be more consumers and one such consumer tripped over this during testing by passing a virHashTablePtr to virObjectIsClass which ends up calling virClassIsDerivedFrom using "obj->klass", which wasn't really a klass object causing one of those bad things to happen. To avoid the future possibility that a non virObject class memory was passed to some virObject* API, let's add a "magic_marker" to the base virObject class that contains a value "0xFEEDBEEF", then any place in the code which would accept or process the base opaque virObjectPtr, compare the magic_marker against 0xFEEDBEEF to make sure this is an object allocated by this code. It is still left up to the caller to handle the failed API calls just as it would be if it passed a NULL opaque pointer anyobj. Signed-off-by: John Ferlan --- src/util/virobject.c | 12 ++++++++---- src/util/virobject.h | 1 + 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/util/virobject.c b/src/util/virobject.c index 9fe4e71..aea8a6d 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -47,10 +47,12 @@ struct _virClass { virObjectDisposeCallback dispose; }; =20 +#define VIR_OBJECT_NOTVALID(obj) (!obj || obj->magic_marker !=3D 0xFEEDBEE= F) + #define VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, objclass) = \ do { = \ virObjectPtr obj =3D anyobj; = \ - if (!obj) = \ + if (VIR_OBJECT_NOTVALID(obj)) = \ VIR_WARN("Object %p is not a virObject class instance", anyobj= );\ else = \ VIR_WARN("Object %p (%s) is not a %s instance", = \ @@ -212,6 +214,7 @@ virObjectNew(virClassPtr klass) return NULL; =20 obj->u.s.magic =3D klass->magic; + obj->magic_marker =3D 0xFEEDBEEF; obj->klass =3D klass; virAtomicIntSet(&obj->u.s.refs, 1); =20 @@ -272,7 +275,7 @@ virObjectUnref(void *anyobj) { virObjectPtr obj =3D anyobj; =20 - if (!obj) + if (VIR_OBJECT_NOTVALID(obj)) return false; =20 bool lastRef =3D virAtomicIntDecAndTest(&obj->u.s.refs); @@ -289,6 +292,7 @@ virObjectUnref(void *anyobj) /* Clear & poison object */ memset(obj, 0, obj->klass->objectSize); obj->u.s.magic =3D 0xDEADBEEF; + obj->magic_marker =3D 0xDEADBEEF; obj->klass =3D (void*)0xDEADBEEF; VIR_FREE(obj); } @@ -311,7 +315,7 @@ virObjectRef(void *anyobj) { virObjectPtr obj =3D anyobj; =20 - if (!obj) + if (VIR_OBJECT_NOTVALID(obj)) return NULL; virAtomicIntInc(&obj->u.s.refs); PROBE(OBJECT_REF, "obj=3D%p", obj); @@ -390,7 +394,7 @@ virObjectIsClass(void *anyobj, virClassPtr klass) { virObjectPtr obj =3D anyobj; - if (!obj) + if (VIR_OBJECT_NOTVALID(obj)) return false; =20 return virClassIsDerivedFrom(obj->klass, klass); diff --git a/src/util/virobject.h b/src/util/virobject.h index f4c292b..89f8050 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -51,6 +51,7 @@ struct _virObject { int refs; } s; } u; + unsigned int magic_marker; virClassPtr klass; }; =20 --=20 2.9.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list