Skip to contentSkip to conversation

out-of-bound array access in drivers/rtc/lib.c rtc_month_days

bug report [email protected] 2 participants 3 messages Feb 19, 2021 – Feb 20, 2021 Real time clock (RTC)

lore
Share & tools
lore
#0 lore raw

Hi Alessandro and Alexandre,
Greetings, I'm a 2nd year PhD student who is interested in using UBSan to the kernel.
Through some experiment, I found a out-of-bound array access in function rtc_month_days.
More specifically, the through the call chain of davinci_rtc_set_time/davinci_rtc_set_alarm -> convert2days -> rtc_month_days,
since davinci_rtc_set_time/davinci_rtc_set_alarm are ioctl functions, thus the 2nd parameter, struct rtc_time *tm, is passed in purely from user-space which can be any value.
And such a value, tm->tm_mon is used directly as an index to a fixed length array, rtc_ydays.
This looks very fishy to me.

Although I know that, syzkaller has applied UBSan to this driver before, and such a simple error cannot evade its detection, I'm still wondering if this is a true error,
and more importantly, if it's not, then why, this will help me understand linux a lot.

Looking forward to your valued response!

Best regards,
Changming Liu

#1 lore raw

Hello,

Quoted 11 lines · On 19/02/2021 13:51:12-0500, [email protected] wrote:

Hi Alessandro and Alexandre,
Greetings, I'm a 2nd year PhD student who is interested in using UBSan to the kernel.
Through some experiment, I found a out-of-bound array access in function rtc_month_days.
More specifically, the through the call chain of davinci_rtc_set_time/davinci_rtc_set_alarm -> convert2days -> rtc_month_days,
since davinci_rtc_set_time/davinci_rtc_set_alarm are ioctl functions, thus the 2nd parameter, struct rtc_time *tm, is passed in purely from user-space which can be any value.

This part is not true and is probably what you are missing, the userspace input is sanitized by the core, see the rtc_valid_tm calls here:
https://elixir.bootlin.com/linux/v5.11/source/drivers/rtc/interface.c#L130
and here:
https://elixir.bootlin.com/linux/v5.11/source/drivers/rtc/interface.c#L457

Quoted 4 lines

And such a value, tm->tm_mon is used directly as an index to a fixed length array, rtc_ydays.
This looks very fishy to me.

Quoted 8 lines

Although I know that, syzkaller has applied UBSan to this driver before, and such a simple error cannot evade its detection, I'm still wondering if this is a true error,
and more importantly, if it's not, then why, this will help me understand linux a lot.

Looking forward to your valued response!

Regards,

Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#2 lore raw

Hi!

Quoted 21 lines

From: Alexandre Belloni <[email protected]>
Sent: Friday, February 19, 2021 7:35 PM
To: [email protected]
Cc: [email protected]; [email protected]
Subject: Re: [bug report] out-of-bound array access in drivers/rtc/lib.c rtc_month_days

Hello,

Quoted 10 lines · On 19/02/2021 13:51:12-0500, [email protected] wrote:

Hi Alessandro and Alexandre,
Greetings, I'm a 2nd year PhD student who is interested in using UBSan to the kernel.
Through some experiment, I found a out-of-bound array access in function rtc_month_days.
More specifically, the through the call chain of davinci_rtc_set_time/davinci_rtc_set_alarm -> convert2days -> rtc_month_days, since davinci_rtc_set_time/davinci_rtc_set_alarm are ioctl functions, thus the 2nd parameter, struct rtc_time *tm, is passed in purely from user-space which can be any value.

Quoted 7 lines

This part is not true and is probably what you are missing, the userspace input is sanitized by the core, see the rtc_valid_tm calls here:
https://elixir.bootlin.com/linux/v5.11/source/drivers/rtc/interface.c#L130
and here:
https://elixir.bootlin.com/linux/v5.11/source/drivers/rtc/interface.c#L457

Thank you so much for pointing this out,
I didn't notice when probing each individual rtc device, there is a devm_rtc_allocate_device which essentially sanitized the ioctl input from the core.
This broaden my understanding of how ioctl works a lot, thank you so much!

Best regards,
Changming

Quoted 4 lines

--
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com