Skip to contentSkip to conversation

lib/date: prevent out-of-bound read in rtc_calc_weekday()

PATCH In review Vincent Stehlé 3 participants 3 messages Oct 14 – Oct 29 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/date: prevent out-of-bound read in rtc_calc_weekday()

Depending on its input structure's tm_mon field value, the rtc_calc_weekday() function may read the month_offset[] array it uses for its computations out of its bounds.
Verify tm_mon on input to prevent that.

While at it, change the month_offset[] array to const as it is only read, and fix a typo and adjust the return code in the function's documentation.

An invalid tm_mon condition can be reproduced with the FAT filesystem on the sandbox for example, with the following command:

  ./test/py/test.py --bd sandbox --build -k test_efi_capsule_fw1
---
 include/rtc.h | 4 ++--
 lib/date.c    | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
include/rtc.h +2 -2
index 22f6d370591..085cb68c584 100644
@@ -309,10 +309,10 @@ static inline bool is_leap_year(unsigned int year)
  * rtc_calc_weekday() - Work out the weekday from a time
  *
  * This only works for the Gregorian calendar - i.e. after 1752 (in the UK).
- * It sets time->tm_wdaay to the correct day of the week.
+ * It sets time->tm_wday to the correct day of the week.
  *
  * @time:	Time to inspect. tm_wday is updated
- * Return: 0 if OK, -EINVAL if the weekday could not be determined
+ * Return: 0 if OK, -1 if the weekday could not be determined
  */
 int rtc_calc_weekday(struct rtc_time *time);
 
lib/date.c +2 -2
index 0deac8a1bee..4e018c22053 100644
@@ -17,7 +17,7 @@
 #define	days_in_year(a)		(leapyear(a) ? 366 : 365)
 #define	days_in_month(a)	(month_days[(a) - 1])
 
-static int month_offset[] = {
+static const int month_offset[] = {
 	0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
 };
 
@@ -30,7 +30,7 @@ int rtc_calc_weekday(struct rtc_time *tm)
 	int last_year;
 	int day;
 
-	if (tm->tm_year < 1753)
+	if (tm->tm_year < 1753 || tm->tm_mon < 1 || tm->tm_mon > 12)
 		return -1;
 	last_year = tm->tm_year - 1;
 

---
base-commit: 32ebe922e574a733d01c3f106fa77d9176bfecad
change-id: 20261014-wday-f3555ab87dc2

Best regards,

Signature · 1 line
Vincent.
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Vincent Stehlé

Depending on its input structure's tm_mon field value, the rtc_calc_weekday() function may read the month_offset[] array it uses for its computations out of its bounds.
Verify tm_mon on input to prevent that.

The FAT date has a 4-bit month field, so fat2rtc() can pass 0 or 13..15 here from a corrupted directory entry.

I built rtc_calc_weekday() on the host with -fsanitize=address,undefined. Before this patch, tm_mon = 0 gives "index -1 out of bounds for type 'int [12]'"; with it, tm_mon = 0, 13 and 15 return -1, and 2026-10-15 still gives tm_wday = 4 (Thursday).

#2 lore raw
In reply to Vincent Stehlé
Quoted 10 lines · Vincent Stehlé

Depending on its input structure's tm_mon field value, the rtc_calc_weekday() function may read the month_offset[] array it uses for its computations out of its bounds.
Verify tm_mon on input to prevent that.

While at it, change the month_offset[] array to const as it is only read, and fix a typo and adjust the return code in the function's documentation.

[...]

Applied to u-boot/main, thanks!

[1/1] lib/date: prevent out-of-bound read in rtc_calc_weekday()

      commit: 3a379940d10804feee0c73fb013143b0ee2ed473
Signature · 1 line
Tom