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

Signed-off-by: Vincent Stehlé <[email protected]>
Cc: Tom Rini <[email protected]>
---
 include/rtc.h | 4 ++--
 lib/date.c    | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/include/rtc.h b/include/rtc.h
index 22f6d370591..085cb68c584 100644
--- a/include/rtc.h
+++ b/include/rtc.h
@@ -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);
 
diff --git a/lib/date.c b/lib/date.c
index 0deac8a1bee..4e018c22053 100644
--- a/lib/date.c
+++ b/lib/date.c
@@ -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,
--  
Vincent.

Reply via email to