Skip to contentSkip to conversation

lib: date: reject out-of-range month in rtc_calc_weekday()

PATCH In review Oscar Deschamps 3 participants 4 messages Oct 27 – Oct 29 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib: date: reject out-of-range month in rtc_calc_weekday()

rtc_calc_weekday() indexes the 12-entry month_offset[] array with tm->tm_mon - 1 but never checks that tm_mon is in 1..12. A caller that derives the month from untrusted data can pass a larger value, causing an out-of-bounds read of month_offset[].

fs/fat reaches this with on-disk data: fat_readdir() calls fat2rtc() on each directory entry's cdate/date/adate fields, and fat2rtc() does

    tm->tm_mon = (date & 0x1e0) >> 5;

The month is a 4-bit field, so it can be 0..15. A directory-entry date word with a month of 13..15 (which a compliant filesystem never writes, but a crafted or corrupted image can contain) makes rtc_calc_weekday() read past month_offset[]. This happens while merely listing a FAT volume (fatls / ls), i.e. on attacker-controlled media before it is validated.

The impact is limited to an out-of-bounds read (a wrong weekday, or on a sanitizer build an abort); there is no write. Reject an out-of-range month, as is already done for the year. This also hardens the other callers (mc146818, efi_runtime, cmd/date).

Found with a libFuzzer harness over the FAT parser (test/fuzz/fat.c).

---
 lib/date.c | 2 ++
 1 file changed, 2 insertions(+)
lib/date.c +2 -0
index 0deac8a1bee..7ac257fcb21 100644
@@ -32,6 +32,8 @@ int rtc_calc_weekday(struct rtc_time *tm)
 
 	if (tm->tm_year < 1753)
 		return -1;
+	if (tm->tm_mon < 1 || tm->tm_mon > 12)
+		return -1;
 	last_year = tm->tm_year - 1;
 
 	/* Number of leap corrections to apply up to end of last year */
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw

Hi Oscar,

Quoted 10 lines · Oscar Deschamps

lib: date: reject out-of-range month in rtc_calc_weekday()

rtc_calc_weekday() indexes the 12-entry month_offset[] array with tm->tm_mon - 1 but never checks that tm_mon is in 1..12. A caller that derives the month from untrusted data can pass a larger value, causing an out-of-bounds read of month_offset[].

fs/fat reaches this with on-disk data: fat_readdir() calls fat2rtc() on each directory entry's cdate/date/adate fields, and fat2rtc() does

Quoted 7 lines
    tm->tm_mon = (date & 0x1e0) >> 5;

The month is a 4-bit field, so it can be 0..15. A directory-entry date word with a month of 13..15 (which a compliant filesystem never writes, but a crafted or corrupted image can contain) makes rtc_calc_weekday() read past month_offset[]. This happens while merely listing a FAT volume (fatls / ls), i.e. on attacker-controlled media before it is validated.

Month 0 is more common and needs no crafted image. The creation and access dates are optional in FAT and many implementations leave them as zero, so a zero date word gives tm_mon = 0 and reads month_offset[-1] on an ordinary volume. Please can you mention this, since the patch fixes it too. Also, fat_readdir() only calls fat2rtc() when EFI_LOADER is enabled, so it would be worth saying that.

Quoted 1 line

Found with a libFuzzer harness over the FAT parser (test/fuzz/fat.c).

There is no test/fuzz/fat.c in the tree - are you planning to send the harness too? A small unit test for rtc_calc_weekday() with months 0 and 13 would be good, perhaps as a follow-up.

Quoted 7 lines

The impact is limited to an out-of-bounds read (a wrong weekday, or on a sanitizer build an abort); there is no write. Reject an out-of-range [...]

 lib/date.c | 2 ++
 1 file changed, 2 insertions(+)

In any case:

Regards,
Simon

#2 lore raw

Thanks for the review.

I should have searched the list before sending: Vincent Stehlé posted the same fix on 2026-10-14, "lib/date: prevent out-of-bound read in rtc_calc_weekday()" (Message-ID [email protected]), already Reviewed-by Hung-Chun Tseng, with the identical bound check. I don't want to duplicate it, please take his and disregard mine. If the month-0 / EFI_LOADER wording here is useful it's welcome in his commit message, but I won't resend a competing version.

Quoted 3 lines · Simon Glass

There is no test/fuzz/fat.c in the tree - are you planning to send the harness too? A small unit test for rtc_calc_weekday() with months 0 and 13 would be good, perhaps as a follow-up.

On the harness you asked about: there is no test/fuzz/fat.c in tree yet. It's a libFuzzer harness I run from the sandbox FUZZ framework against the generic fs layer (it's grown to cover EROFS and SquashFS too). It needs cleanup before it's fit to post, but I'll be glad to share it.

For the unit test you suggested, I have one for rtc_calc_weekday covering months 0, 13 and 15 (rejected) plus valid months; it fails without the bound check and passes with it. I'll send it as a follow-up once the fix is in.

Thanks,
Oscar.

#3 lore raw
In reply to Oscar Deschamps
Quoted 16 lines · Oscar Deschamps

rtc_calc_weekday() indexes the 12-entry month_offset[] array with tm->tm_mon - 1 but never checks that tm_mon is in 1..12. A caller that derives the month from untrusted data can pass a larger value, causing an out-of-bounds read of month_offset[].

fs/fat reaches this with on-disk data: fat_readdir() calls fat2rtc() on each directory entry's cdate/date/adate fields, and fat2rtc() does

    tm->tm_mon = (date & 0x1e0) >> 5;

The month is a 4-bit field, so it can be 0..15. A directory-entry date word with a month of 13..15 (which a compliant filesystem never writes, but a crafted or corrupted image can contain) makes rtc_calc_weekday() read past month_offset[]. This happens while merely listing a FAT volume (fatls / ls), i.e. on attacker-controlled media before it is validated.

Quoted 8 lines

The impact is limited to an out-of-bounds read (a wrong weekday, or on a sanitizer build an abort); there is no write. Reject an out-of-range month, as is already done for the year. This also hardens the other callers (mc146818, efi_runtime, cmd/date).

Found with a libFuzzer harness over the FAT parser (test/fuzz/fat.c).

This was reported recently with:
https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/
which also covers what Simon is talking about I believe.

Signature · 1 line
Tom