[PATCH] fat: fat2rtc: Sanitize timestamps

Richard Weinberger richard at nod.at
Fri Jul 12 10:24:54 CEST 2024


Make sure that tm_mday and tm_mon are within the expected
range. Upper layers such as rtc_calc_weekday() will use
them as lookup keys for arrays and this can cause out of
bounds memory accesses.

Signed-off-by: Richard Weinberger <richard at nod.at>
---
 fs/fat/fat.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/fs/fat/fat.c b/fs/fat/fat.c
index 2dd9d4e72d..f9096e8932 100644
--- a/fs/fat/fat.c
+++ b/fs/fat/fat.c
@@ -1253,8 +1253,9 @@ out:
  */
 static void __maybe_unused fat2rtc(u16 date, u16 time, struct rtc_time *tm)
 {
-	tm->tm_mday = date & 0x1f;
-	tm->tm_mon = (date & 0x1e0) >> 5;
+	tm->tm_mday = max(1, date & 0x1f);
+	tm->tm_mon = clamp((date & 0x1e0) >> 5, 1, 12);
+
 	tm->tm_year = (date >> 9) + 1980;
 
 	tm->tm_sec = (time & 0x1f) << 1;
-- 
2.35.3



More information about the U-Boot mailing list