From patchwork Tue May 3 12:10:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cristian Marussi X-Patchwork-Id: 12835722 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E03CBC433EF for ; Tue, 3 May 2022 12:12:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=Eh1eSHLOAPgvdaLxeG6QQNtD68G+HeAoQFm78ilQ2uw=; b=p8t9OFY/Y+4Iva AzZlzhRLq70Y0nHcpGcRr0+h8I9mxZe2g2VpO8s77TWpd1gDKq9rUUvI+8uan5PDCerPA80M9q1Qj e9KU7J3RJpNJeRiNuco9GqiUb+oIDIc8Mq9ey+zK3fwn4u57cXxFIQ+DnmVrgT1oO0PBxUmoC3KU5 wN/0kd7fWWcAz1Hc2LX4g66B35M+Y8oY6+rI77c7q6DNZVAj12Yovjcjw+J0bVt05wMyUBwMzCuwl iV5CarSnjml/NGq/a3IHHBbQL/R4tI2opXxS35z/Y00cNVEfednPctXsplq8QbbvtOIZZS4BAbKUC VzuQWdWGWYQ1NnZprXXQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nlrN3-005hVo-0Z; Tue, 03 May 2022 12:11:13 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nlrMz-005hUc-1p for linux-arm-kernel@lists.infradead.org; Tue, 03 May 2022 12:11:10 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 7FDCE1042; Tue, 3 May 2022 05:11:04 -0700 (PDT) Received: from e120937-lin.home (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 8EE5A3F73D; Tue, 3 May 2022 05:11:03 -0700 (PDT) From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: sudeep.holla@arm.com, Cristian Marussi , kernel test robot , Dan Carpenter Subject: [PATCH] firmware: arm_scmi: Fix late checks on pointer dereference Date: Tue, 3 May 2022 13:10:47 +0100 Message-Id: <20220503121047.3590340-1-cristian.marussi@arm.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220503_051109_174711_CF67D7E0 X-CRM114-Status: GOOD ( 12.26 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org A few dereferences could happen before the iterator pointer argument was checked for NULL, causing the following smatch warnings: drivers/firmware/arm_scmi/driver.c:1214 scmi_iterator_run() warn: variable dereferenced before check 'i' (see line 1210) Fix by moving the checks early and dropping some unneeded local references. No functional change. Reported-by: kernel test robot Reported-by: Dan Carpenter Signed-off-by: Cristian Marussi --- drivers/firmware/arm_scmi/driver.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c index c4960fd3df75..c1922bd650ae 100644 --- a/drivers/firmware/arm_scmi/driver.c +++ b/drivers/firmware/arm_scmi/driver.c @@ -1205,18 +1205,21 @@ static void *scmi_iterator_init(const struct scmi_protocol_handle *ph, static int scmi_iterator_run(void *iter) { int ret = -EINVAL; + struct scmi_iterator_ops *iops; + const struct scmi_protocol_handle *ph; + struct scmi_iterator_state *st; struct scmi_iterator *i = iter; - struct scmi_iterator_state *st = &i->state; - struct scmi_iterator_ops *iops = i->ops; - const struct scmi_protocol_handle *ph = i->ph; - const struct scmi_xfer_ops *xops = ph->xops; - if (!i) + if (!i || !i->ops || !i->ph) return ret; + iops = i->ops; + ph = i->ph; + st = &i->state; + do { iops->prepare_message(i->msg, st->desc_index, i->priv); - ret = xops->do_xfer(ph, i->t); + ret = ph->xops->do_xfer(ph, i->t); if (ret) break; @@ -1240,7 +1243,7 @@ static int scmi_iterator_run(void *iter) } st->desc_index += st->num_returned; - xops->reset_rx_to_maxsz(ph, i->t); + ph->xops->reset_rx_to_maxsz(ph, i->t); /* * check for both returned and remaining to avoid infinite * loop due to buggy firmware @@ -1249,7 +1252,7 @@ static int scmi_iterator_run(void *iter) out: /* Finalize and destroy iterator */ - xops->xfer_put(ph, i->t); + ph->xops->xfer_put(ph, i->t); devm_kfree(ph->dev, i); return ret;