From: Alexandre Belloni <alexandre.belloni@bootlin.com>
To: ZhangXiaoxu <zhangxiaoxu5@huawei.com>
Cc: a.zummo@towertech.it, linux-rtc@vger.kernel.org
Subject: Re: [PATCH] rtc: Fix UBSAN overflow warning
Date: Thu, 20 Dec 2018 12:07:31 +0100 [thread overview]
Message-ID: <20181220110731.GC2188@piout.net> (raw)
In-Reply-To: <1545298616-62881-1-git-send-email-zhangxiaoxu5@huawei.com>
Hi,
On 20/12/2018 17:36:56+0800, ZhangXiaoxu wrote:
> Users may call 'ioctl' and pass a very big value on 'tm->tm_year'.
> It can be overflowed in 'int' after add 1900.
> In function 'rtc_month_days' and 'mktime64', also treated it as an
> 'unsigned' parameter.
>
> UBSAN: Undefined behaviour in drivers/rtc/rtc-lib.c:103:59
> signed integer overflow:
> 2147483647 + 1900 cannot be represented in type 'int'
>
> UBSAN: Undefined behaviour in drivers/rtc/rtc-lib.c:119:30
> signed integer overflow:
> 2147483647 + 1900 cannot be represented in type 'int'
>
> So, covert it to 'unsigned' explicitly.
>
> Signed-off-by: ZhangXiaoxu <zhangxiaoxu5@huawei.com>
> ---
> drivers/rtc/rtc-lib.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/rtc/rtc-lib.c b/drivers/rtc/rtc-lib.c
> index ef160da..9714cb3 100644
> --- a/drivers/rtc/rtc-lib.c
> +++ b/drivers/rtc/rtc-lib.c
> @@ -100,7 +100,7 @@ int rtc_valid_tm(struct rtc_time *tm)
> if (tm->tm_year < 70
> || ((unsigned)tm->tm_mon) >= 12
> || tm->tm_mday < 1
> - || tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year + 1900)
> + || tm->tm_mday > rtc_month_days(tm->tm_mon, ((unsigned)tm->tm_year + 1900))
Isn't the cast to unsigned done by rtc_month_days enough?
> || ((unsigned)tm->tm_hour) >= 24
> || ((unsigned)tm->tm_min) >= 60
> || ((unsigned)tm->tm_sec) >= 60)
> @@ -116,8 +116,8 @@ EXPORT_SYMBOL(rtc_valid_tm);
> */
> time64_t rtc_tm_to_time64(struct rtc_time *tm)
> {
> - return mktime64(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
> - tm->tm_hour, tm->tm_min, tm->tm_sec);
> + return mktime64(((unsigned)tm->tm_year + 1900), tm->tm_mon + 1,
> + tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
mktime64 will fail way before tm->tm_year + 1900 overflows an int and
also it already takes an unsigned int for year so I'm not sure this cast
is actually necessary.
> }
> EXPORT_SYMBOL(rtc_tm_to_time64);
>
> --
> 2.7.4
>
--
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
next prev parent reply other threads:[~2018-12-20 11:07 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-12-20 9:36 [PATCH] rtc: Fix UBSAN overflow warning ZhangXiaoxu
2018-12-20 11:07 ` Alexandre Belloni [this message]
2018-12-20 14:11 ` zhangxiaoxu (A)
2019-01-10 21:03 ` 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=20181220110731.GC2188@piout.net \
--to=alexandre.belloni@bootlin.com \
--cc=a.zummo@towertech.it \
--cc=linux-rtc@vger.kernel.org \
--cc=zhangxiaoxu5@huawei.com \
/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.