From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.3 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DCEBAC43460 for ; Thu, 22 Apr 2021 11:34:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9751F6143B for ; Thu, 22 Apr 2021 11:34:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236046AbhDVLfc (ORCPT ); Thu, 22 Apr 2021 07:35:32 -0400 Received: from foss.arm.com ([217.140.110.172]:50284 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230510AbhDVLfa (ORCPT ); Thu, 22 Apr 2021 07:35:30 -0400 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 B25D113A1; Thu, 22 Apr 2021 04:34:55 -0700 (PDT) Received: from e120937-lin (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id B75AC3F774; Thu, 22 Apr 2021 04:34:54 -0700 (PDT) Date: Thu, 22 Apr 2021 12:34:52 +0100 From: Cristian Marussi To: Sudeep Holla Cc: Dan Carpenter , "Jon Medhurst (Tixy)" , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org Subject: Re: [PATCH] firmware: arm_scpi: prevent ternary sign expansion bug Message-ID: <20210422113451.GG43717@e120937-lin> References: <20210422101709.GF43717@e120937-lin> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210422101709.GF43717@e120937-lin> User-Agent: Mutt/1.9.4 (2018-02-28) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Apr 22, 2021 at 11:17:09AM +0100, Cristian Marussi wrote: > Hi, > > On Thu, Apr 22, 2021 at 12:02:29PM +0300, Dan Carpenter wrote: > > How type promotion works in ternary expressions is a bit tricky. > > The problem is that scpi_clk_get_val() returns longs, "ret" is a int > > which holds a negative error code, and le32_to_cpu() is an unsigned int. > > We want the negative error code to be cast to a negative long. But > > because le32_to_cpu() is an u32 then "ret" is type promoted to u32 and > > becomes a high positive and then it is promoted to long and it is still > > a high positive value. > > > > Fix this by getting rid of the ternary. > > I wonder how/if the callers up in the stack check/expect ever effectively for a > 2-complement negative value inside the returned unsigned long...given that this > plugs finally into CLK framework struct clk_ops.recalc_rate via clk-scpi.c which > also expects unsigned long....but that's another story. > > FWIW regarding this patch: > > Reviewed-by: Cristian Marussi > > Thanks > > Cristian @Sudeep, as a second though, looking at .recalc_rate() definition inside include/linux/clk-provider.h:struct clk_ops which is the direct caller of this SCPI clk function, I wonder if, instead, on error we should not return here just ZERO as the returned clock rate value as in: if (ret) return 0; given that the error code returned inside the unsigned long won't be ever considerd as such apparently, so not sure if it'd be worst to return a very big fake value or zero... Thanks Cristian > > > > Fixes: 8cb7cf56c9fe ("firmware: add support for ARM System Control and Power Interface(SCPI) protocol") > > Signed-off-by: Dan Carpenter > > --- > > drivers/firmware/arm_scpi.c | 4 +++- > > 1 file changed, 3 insertions(+), 1 deletion(-) > > > > diff --git a/drivers/firmware/arm_scpi.c b/drivers/firmware/arm_scpi.c > > index d0dee37ad522..3bf61854121d 100644 > > --- a/drivers/firmware/arm_scpi.c > > +++ b/drivers/firmware/arm_scpi.c > > @@ -552,8 +552,10 @@ static unsigned long scpi_clk_get_val(u16 clk_id) > > > > ret = scpi_send_message(CMD_GET_CLOCK_VALUE, &le_clk_id, > > sizeof(le_clk_id), &rate, sizeof(rate)); > > + if (ret) > > + return ret; > > > > - return ret ? ret : le32_to_cpu(rate); > > + return le32_to_cpu(rate); > > } > > > > static int scpi_clk_set_val(u16 clk_id, unsigned long rate) > > -- > > 2.30.2 > > From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.3 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 18FC6C433B4 for ; Thu, 22 Apr 2021 11:36:50 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B2DE061426 for ; Thu, 22 Apr 2021 11:36:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B2DE061426 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=arm.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References:Message-ID: Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=M3zOWoQIbVAG7TGY0qKEy5gsiw28DNyd0+esT8HbH9k=; b=EHDIR51YtjMslWkY5i+b9EOSJ e88LTpHgWRExKDdMxnU4TlZS1bJMX92mn2s3IaoLfEveSEQCKQ9st8e+X0K/jAl2Oitpx3SLaB62n dUQ3VT1Em6Qa1carOx/+J3qZcJ6IItm7xbhFeqmAnhSZtXbLyWbDp7XwVOGHm0xquRkRUpp7OuxTy VO+EFrcq7dSEweEIZLaO81mKnPfmnwr0ZmmKv7CVJ6eN4eAIeNYC556w49g7UOyqxBmsUG+BEFEJa eSn14XBYXfhOTtY8JROAi8uIhCuwwTiYQ8K1RylpSHzCnSAMM3rhqJQ1hM81wxl/VQr66nBkOyt4z llKJ1ZXWw==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lZXbu-00GbUe-50; Thu, 22 Apr 2021 11:35:06 +0000 Received: from bombadil.infradead.org ([2607:7c80:54:e::133]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lZXbq-00GbRr-1C for linux-arm-kernel@desiato.infradead.org; Thu, 22 Apr 2021 11:35:03 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=8Qb8E73oC+UannJblpmSUyKcjjvktlFg53grp25CjbA=; b=M4a+9y9MFewJL7/M3Z6aItXrVh VbEV/hLzXbb53/UcehvbNl4onKHsbn/1dImN1uOc4+1QjpOwkY5FLWczorvdO4w4YSD56qDUctSmO Kblu6HU14RRtZjoO2Ro79atfNlTsFGOlH+RT6WzIKiRpPWdRm6rGBHYFLyaPVqHoIb+vc36vQN3QN kqjfF5bsxNLmWyMoyNdIaV1yqR0beqkWqciygYUJyA4I9SzioIOlYvKjb+aav+BUgRaDRUwgqBU4u 9ep7ZelEGWYkNXm5iEI+OsARQHIxhssOdm7WKYk1uDBjTMoPt5SjriTYS74WQuQDzT0CGSNnGNk5A 9+tLKlOg==; Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lZXbn-00DcQM-Ag for linux-arm-kernel@lists.infradead.org; Thu, 22 Apr 2021 11:35:00 +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 B25D113A1; Thu, 22 Apr 2021 04:34:55 -0700 (PDT) Received: from e120937-lin (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id B75AC3F774; Thu, 22 Apr 2021 04:34:54 -0700 (PDT) Date: Thu, 22 Apr 2021 12:34:52 +0100 From: Cristian Marussi To: Sudeep Holla Cc: Dan Carpenter , "Jon Medhurst (Tixy)" , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org Subject: Re: [PATCH] firmware: arm_scpi: prevent ternary sign expansion bug Message-ID: <20210422113451.GG43717@e120937-lin> References: <20210422101709.GF43717@e120937-lin> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20210422101709.GF43717@e120937-lin> User-Agent: Mutt/1.9.4 (2018-02-28) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210422_043459_435727_6CFDD87F X-CRM114-Status: GOOD ( 27.24 ) 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: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Thu, Apr 22, 2021 at 11:17:09AM +0100, Cristian Marussi wrote: > Hi, > > On Thu, Apr 22, 2021 at 12:02:29PM +0300, Dan Carpenter wrote: > > How type promotion works in ternary expressions is a bit tricky. > > The problem is that scpi_clk_get_val() returns longs, "ret" is a int > > which holds a negative error code, and le32_to_cpu() is an unsigned int. > > We want the negative error code to be cast to a negative long. But > > because le32_to_cpu() is an u32 then "ret" is type promoted to u32 and > > becomes a high positive and then it is promoted to long and it is still > > a high positive value. > > > > Fix this by getting rid of the ternary. > > I wonder how/if the callers up in the stack check/expect ever effectively for a > 2-complement negative value inside the returned unsigned long...given that this > plugs finally into CLK framework struct clk_ops.recalc_rate via clk-scpi.c which > also expects unsigned long....but that's another story. > > FWIW regarding this patch: > > Reviewed-by: Cristian Marussi > > Thanks > > Cristian @Sudeep, as a second though, looking at .recalc_rate() definition inside include/linux/clk-provider.h:struct clk_ops which is the direct caller of this SCPI clk function, I wonder if, instead, on error we should not return here just ZERO as the returned clock rate value as in: if (ret) return 0; given that the error code returned inside the unsigned long won't be ever considerd as such apparently, so not sure if it'd be worst to return a very big fake value or zero... Thanks Cristian > > > > Fixes: 8cb7cf56c9fe ("firmware: add support for ARM System Control and Power Interface(SCPI) protocol") > > Signed-off-by: Dan Carpenter > > --- > > drivers/firmware/arm_scpi.c | 4 +++- > > 1 file changed, 3 insertions(+), 1 deletion(-) > > > > diff --git a/drivers/firmware/arm_scpi.c b/drivers/firmware/arm_scpi.c > > index d0dee37ad522..3bf61854121d 100644 > > --- a/drivers/firmware/arm_scpi.c > > +++ b/drivers/firmware/arm_scpi.c > > @@ -552,8 +552,10 @@ static unsigned long scpi_clk_get_val(u16 clk_id) > > > > ret = scpi_send_message(CMD_GET_CLOCK_VALUE, &le_clk_id, > > sizeof(le_clk_id), &rate, sizeof(rate)); > > + if (ret) > > + return ret; > > > > - return ret ? ret : le32_to_cpu(rate); > > + return le32_to_cpu(rate); > > } > > > > static int scpi_clk_set_val(u16 clk_id, unsigned long rate) > > -- > > 2.30.2 > > _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel