Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sX1wD-0014WL-9r for pgsql-hackers@arkaria.postgresql.org; Thu, 25 Jul 2024 17:07:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sX1wB-00HEMF-Jc for pgsql-hackers@arkaria.postgresql.org; Thu, 25 Jul 2024 17:07:31 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sX1wA-00HEM7-G1 for pgsql-hackers@lists.postgresql.org; Thu, 25 Jul 2024 17:07:31 +0000 Received: from fhigh5-smtp.messagingengine.com ([103.168.172.156]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sX1w7-001PLX-6t for pgsql-hackers@lists.postgresql.org; Thu, 25 Jul 2024 17:07:29 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 99B8A11401C4; Thu, 25 Jul 2024 13:07:24 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Thu, 25 Jul 2024 13:07:24 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1721927244; x= 1722013644; bh=Lqdhjp8kJ6M5ZrkCQ5za3UDPNNhA927URZVxQWVl05k=; b=O arvtiyCcAM1wJxn4/38WE93Fh1nnb5qup1Mveuiso9Ucd/hOPZPiLTlmEoQP+/eJ ZqUebWjIe/b/Gd8Ie8iBME/UTIClrur55fmSLgEudFani0+5JIj5aDVAtSbz1P4O qx1rdBqMhO5bJp1P2NTwSvzLErbMIqPZ4w2f1R6U/9rM37hp5pAaWIu1d9XtKkTq trchadJ1jCNdNai7yieo9prjwY+ZfZMST9EfQ1eSp+HmlGXV3Y7kXf5i/n6r5zPk o9HKvfdSJToRVgclyJMrN04DnUUmzQR79ePcBsIMC/rzI2OO7gTC6kaRia+h/XNS kdQ1G0bDw1YoILfDbVjZg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrieefgddutdelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkgggtugfgjgesthekredttddtjeenucfhrhhomheptehlvhgr rhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh eqnecuggftrfgrthhtvghrnhepvdektdffudfftdffffehfffhjeejhffgieeuueekjeek fffgudffhfduffffueevnecuffhomhgrihhnpegvnhhtvghrphhrihhsvggusgdrtghomh enucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrlhhv hhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhgpdhnsggprhgtphhtthhopedt X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 25 Jul 2024 13:07:23 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1721927241; bh=dx2GT51Gnqy0BVUVXfXUv61Srapqp1PvAMXTjn93vnk=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=nnc2O70zRAbZvx1FW1Vh/ZJdvPYbG7+EL4+gqgdrH4TZsIbsazKGdonPg6He15OSU XJla+jJO3MpysNldiCjuTAu+3o6VLpFvNEBC0/8wRhsvMLniOPucS4H23N0fWL7guv N1YzQWGJuSdaGhhKo9NTwZJbkaXHkbjGfCQMy8VIW4bPZLAD7O8UkMoSH4zlzLA8cr n+XZNEsMPWe85tPYc+yHPOW+xdodyNo9la3EW1P4D6/c5xcX/pccOQ9Mp3gwKtE1Em cOXgKthiN+dmu/zF3YwCMYdIB1xqNCSOhrA5lSuIvYykck0kpQnasrwb1DuNQL6tsV pYnpj2DRNCKbg== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id CDC22BE; Thu, 25 Jul 2024 19:07:21 +0200 (CEST) Date: Thu, 25 Jul 2024 19:07:21 +0200 From: Alvaro Herrera To: Alexander Kuznetsov Cc: pgsql-hackers@lists.postgresql.org, nickel@altlinux.org, egori@altlinux.org Subject: Re: Possible null pointer dereference in afterTriggerAddEvent() Message-ID: <202407251707.tkkrcs7c4eso@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <6d0323c3-3f5d-4137-af73-98a5ab90e77c@altlinux.org> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2024-Jul-25, Alexander Kuznetsov wrote: Hello Alexander, > In src/backend/commands/trigger.c:4031, there is an > afterTriggerAddEvent() function. The variable chunk is assigned the > value of events->tail at line 4050. Subsequently, chunk is compared to > NULL at lines 4051 and 4079, indicating that events->tail could > potentially be NULL. > > However, at line 4102, we dereference events->tail by accessing > events->tail->next without first checking if it is NULL. Thanks for reporting this. I think the unwritten assumption is that ->tail and ->head are NULL or not simultaneously, so testing for one of them is equivalent to testing both. Failing to comply with this would be data structure corruption. > To address this issue, I propose at least adding an assertion to > ensure that events->tail != NULL before the dereference. The suggested > patch is included in the attachment. Hmm, this doesn't actually change any behavior AFAICS. If events->tail is NULL and we reach that code, then the dereference to get ->next is going to crash, whether the assertion is there or not. Maybe for sanity (and perhaps for Svace compliance) we could do it the other way around, i.e. by testing events->tail for nullness instead of events->head, then add the assertion: if (events->tail == NULL) { Assert(events->head == NULL); events->head = chunk; } else events->tail->next = chunk; This way, it's not wholly redundant. That said, I'm not sure we actually *need* to change this. -- Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/ "El Maquinismo fue proscrito so pena de cosquilleo hasta la muerte" (Ijon Tichy en Viajes, Stanislaw Lem)