All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexandre Belloni <alexandre.belloni@free-electrons.com>
To: Colin King <colin.king@canonical.com>
Cc: Alessandro Zummo <a.zummo@towertech.it>,
	linux-rtc@vger.kernel.org, kernel-janitors@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH][V2] rtc: tx4939: avoid unintended sign extension on a 24 bit shift
Date: Thu, 15 Feb 2018 20:44:53 +0000	[thread overview]
Message-ID: <20180215204453.GE14177@piout.net> (raw)
In-Reply-To: <20180215193614.28684-1-colin.king@canonical.com>

On 15/02/2018 at 19:36:14 +0000, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> The shifting of buf[5] by 24 bits to the left will be promoted to
> a 32 bit signed int and then sign-extended to an unsigned long. If
> the top bit of buf[5] is set then all then all the upper bits sec
> end up as also being set because of the sign-extension. Fix this by
> casting buf[5] to an unsigned long before the shift.
> 

The timing of the discovery of this issue is suspicious. I believe it is
because I just enabled COMPILE_TEST on that driver and now this gets
compiled on a 64bit architecture.

Can I ask on which architecture this is an issue? I don't think (and a
small test program confirms) x86 does the sign extension because both
sec and buf are unsigned.

> Detected by CoverityScan, CID#1465292 ("Unintended sign extension")
> 
> Fixes: 0e1492330cd2 ("rtc: add rtc-tx4939 driver")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  drivers/rtc/rtc-tx4939.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/rtc/rtc-tx4939.c b/drivers/rtc/rtc-tx4939.c
> index feededce3ded..1f351308afdc 100644
> --- a/drivers/rtc/rtc-tx4939.c
> +++ b/drivers/rtc/rtc-tx4939.c
> @@ -109,7 +109,8 @@ static int tx4939_rtc_read_time(struct device *dev, struct rtc_time *tm)
>  	for (i = 2; i < 6; i++)
>  		buf[i] = __raw_readl(&rtcreg->dat);
>  	spin_unlock_irq(&pdata->lock);
> -	sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2];
> +	sec = ((unsigned long)buf[5] << 24) | (buf[4] << 16) |
> +		(buf[3] << 8) | buf[2];
>  	rtc_time_to_tm(sec, tm);
>  	return rtc_valid_tm(tm);
>  }
> @@ -170,7 +171,8 @@ static int tx4939_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
>  	alrm->enabled = (ctl & TX4939_RTCCTL_ALME) ? 1 : 0;
>  	alrm->pending = (ctl & TX4939_RTCCTL_ALMD) ? 1 : 0;
>  	spin_unlock_irq(&pdata->lock);
> -	sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2];
> +	sec = ((unsigned long)buf[5] << 24) | (buf[4] << 16) |
> +		(buf[3] << 8) | buf[2];
>  	rtc_time_to_tm(sec, &alrm->time);
>  	return rtc_valid_tm(&alrm->time);
>  }
> -- 
> 2.15.1
> 

-- 
Alexandre Belloni, Bootlin (formerly Free Electrons)
Embedded Linux and Kernel engineering
http://bootlin.com

WARNING: multiple messages have this Message-ID (diff)
From: Alexandre Belloni <alexandre.belloni@free-electrons.com>
To: Colin King <colin.king@canonical.com>
Cc: Alessandro Zummo <a.zummo@towertech.it>,
	linux-rtc@vger.kernel.org, kernel-janitors@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH][V2] rtc: tx4939: avoid unintended sign extension on a 24 bit shift
Date: Thu, 15 Feb 2018 21:44:53 +0100	[thread overview]
Message-ID: <20180215204453.GE14177@piout.net> (raw)
In-Reply-To: <20180215193614.28684-1-colin.king@canonical.com>

On 15/02/2018 at 19:36:14 +0000, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> The shifting of buf[5] by 24 bits to the left will be promoted to
> a 32 bit signed int and then sign-extended to an unsigned long. If
> the top bit of buf[5] is set then all then all the upper bits sec
> end up as also being set because of the sign-extension. Fix this by
> casting buf[5] to an unsigned long before the shift.
> 

The timing of the discovery of this issue is suspicious. I believe it is
because I just enabled COMPILE_TEST on that driver and now this gets
compiled on a 64bit architecture.

Can I ask on which architecture this is an issue? I don't think (and a
small test program confirms) x86 does the sign extension because both
sec and buf are unsigned.

> Detected by CoverityScan, CID#1465292 ("Unintended sign extension")
> 
> Fixes: 0e1492330cd2 ("rtc: add rtc-tx4939 driver")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  drivers/rtc/rtc-tx4939.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/rtc/rtc-tx4939.c b/drivers/rtc/rtc-tx4939.c
> index feededce3ded..1f351308afdc 100644
> --- a/drivers/rtc/rtc-tx4939.c
> +++ b/drivers/rtc/rtc-tx4939.c
> @@ -109,7 +109,8 @@ static int tx4939_rtc_read_time(struct device *dev, struct rtc_time *tm)
>  	for (i = 2; i < 6; i++)
>  		buf[i] = __raw_readl(&rtcreg->dat);
>  	spin_unlock_irq(&pdata->lock);
> -	sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2];
> +	sec = ((unsigned long)buf[5] << 24) | (buf[4] << 16) |
> +		(buf[3] << 8) | buf[2];
>  	rtc_time_to_tm(sec, tm);
>  	return rtc_valid_tm(tm);
>  }
> @@ -170,7 +171,8 @@ static int tx4939_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
>  	alrm->enabled = (ctl & TX4939_RTCCTL_ALME) ? 1 : 0;
>  	alrm->pending = (ctl & TX4939_RTCCTL_ALMD) ? 1 : 0;
>  	spin_unlock_irq(&pdata->lock);
> -	sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2];
> +	sec = ((unsigned long)buf[5] << 24) | (buf[4] << 16) |
> +		(buf[3] << 8) | buf[2];
>  	rtc_time_to_tm(sec, &alrm->time);
>  	return rtc_valid_tm(&alrm->time);
>  }
> -- 
> 2.15.1
> 

-- 
Alexandre Belloni, Bootlin (formerly Free Electrons)
Embedded Linux and Kernel engineering
http://bootlin.com

  reply	other threads:[~2018-02-15 20:44 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-15 19:36 [PATCH][V2] rtc: tx4939: avoid unintended sign extension on a 24 bit shift Colin King
2018-02-15 20:44 ` Alexandre Belloni [this message]
2018-02-15 20:44   ` Alexandre Belloni
2018-02-16 15:24   ` Alexandre Belloni
2018-02-16 15:24     ` Alexandre Belloni
2018-02-16 15:26     ` Colin Ian King
2018-02-16 23:52 ` Alexandre Belloni
2018-02-16 23:52   ` Alexandre Belloni

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180215204453.GE14177@piout.net \
    --to=alexandre.belloni@free-electrons.com \
    --cc=a.zummo@towertech.it \
    --cc=colin.king@canonical.com \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rtc@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.