From d761828fd7f84c264e557fff3a5703a5abb17be7 Mon Sep 17 00:00:00 2001 From: Arseniy Seroka <2179419+jagajaga@users.noreply.github.com> Date: Thu, 19 Nov 2026 18:08:30 +0000 Subject: [PATCH] fix(gentime): reject out-of-range dates and zone offsets parse_gentime() did not range-check the fields of a Generalized Time string, so timegm()/mktime() silently normalized them and visudo reported "parsed OK". A typo in a NOTAFTER (or sudoNotAfter) value could extend a time-limited rule, e.g. NOTAFTER=20260230235959Z -> expires March 2 (no February 30) NOTAFTER=20269999999999Z -> expires in 2034 Reject month, day (including days that do not exist in the month), hour, minute, second and time zone offset values outside the ranges allowed by RFC 4517. Leap days and leap seconds are still accepted, and so is 24:00:00 as the end of the day (ISO 8601), which already meant 00:00:00 of the next day; any other hour 24 is rejected. Add the corresponding check_gentime cases. A sudoers rule with such a date is now a syntax error: with the default error_recovery that rule is skipped, with error_recovery=false the file is rejected. An LDAP entry with such a date is treated as expired, as for any other invalid sudoNotBefore/sudoNotAfter value. --- plugins/sudoers/gentime.c | 39 +++++++++++++++++++ .../sudoers/regress/parser/check_gentime.c | 23 +++++++++++ 2 files changed, 62 insertions(+) diff --git a/plugins/sudoers/gentime.c b/plugins/sudoers/gentime.c index 2a4526f025..3a7e8c2163 100644 --- a/plugins/sudoers/gentime.c +++ b/plugins/sudoers/gentime.c @@ -38,6 +38,19 @@ # define timegm(_t) sudo_timegm(_t) #endif +/* + * Number of days in month mon (1-12) of year (Gregorian calendar). + */ +static int +days_in_month(int year, int mon) +{ + static const int mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + + if (mon == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) + return 29; + return mdays[mon - 1]; +} + /* * Parse a timestamp in Generalized Time format as per RFC4517. * E.g. yyyymmddHHMMSS.FZ or yyyymmddHHMMSS.F[+-]TZOFF @@ -76,10 +89,31 @@ parse_gentime(const char *timestr) debug_return_time_t(-1); } + /* + * Reject out-of-range fields (RFC 4517): timegm()/mktime() would + * otherwise silently normalize them, e.g. February 30 to March 2. + * Checked before any fraction is applied; 60 seconds is a leap second. + * As in ISO 8601, 24:00:00 is accepted as the end of the day. + */ + if (tm.tm_mon < 1 || tm.tm_mon > 12 || tm.tm_mday < 1 || + tm.tm_mday > days_in_month(tm.tm_year, tm.tm_mon) || + tm.tm_hour > 24 || tm.tm_min > 59 || tm.tm_sec > 60 || + (tm.tm_hour == 24 && (tm.tm_min != 0 || tm.tm_sec != 0))) { + sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, + "out of range value in general time string %s", timestr); + debug_return_time_t(-1); + } + /* Parse optional fractional hours/minute/second if present. */ cp = timestr + len; if ((cp[0] == '.' || cp[0] == ',') && isdigit((unsigned char)cp[1])) { int frac = cp[1] - '0'; + if (tm.tm_hour == 24) { + /* 24:00:00 is the end of the day, there is nothing after it. */ + sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, + "fraction after 24:00:00 in general time string %s", timestr); + debug_return_time_t(-1); + } switch (items) { case 4: /* convert fractional hour -> minutes */ @@ -122,6 +156,11 @@ parse_gentime(const char *timestr) timestr, items); debug_return_time_t(-1); } + if (hour > 23 || min > 59) { + sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, + "time zone offset out of range in %s", timestr); + debug_return_time_t(-1); + } if (*cp == '-') tzoff = -((hour * 60) + min) * 60; else diff --git a/plugins/sudoers/regress/parser/check_gentime.c b/plugins/sudoers/regress/parser/check_gentime.c index 2854dc7cb1..f362068e48 100644 --- a/plugins/sudoers/regress/parser/check_gentime.c +++ b/plugins/sudoers/regress/parser/check_gentime.c @@ -57,6 +57,29 @@ const struct gentime_test { { "2017021408.50-0500", 1487079000 }, { "20170214083000.Z", -1 }, { "20170214Z", -1 }, + /* out-of-range fields must be rejected, not normalized */ + { "20260230235959Z", -1 }, /* there is no February 30 */ + { "20250229120000Z", -1 }, /* 2025 is not a leap year */ + { "20260431120000Z", -1 }, /* April has 30 days */ + { "20260100120000Z", -1 }, /* day 0 */ + { "20261301000000Z", -1 }, /* month 13 */ + { "20260001000000Z", -1 }, /* month 0 */ + { "2026010125Z", -1 }, /* hour 25 */ + { "202601012401Z", -1 }, /* only 24:00:00 is allowed */ + { "20260101240001Z", -1 }, + { "2026010124.5Z", -1 }, /* nothing comes after 24:00:00 */ + { "20260230240000Z", -1 }, /* still no February 30 */ + { "202601011260Z", -1 }, /* minute 60 */ + { "20260101120061Z", -1 }, /* second 61 */ + { "20269999999999Z", -1 }, + { "2026010112+2400", -1 }, /* zone hour 24 */ + { "2026010112-0060", -1 }, /* zone minute 60 */ + /* edge cases that are valid */ + { "20240229120000Z", 1709208000 }, /* leap day */ + { "20261231235960Z", 1798761600 }, /* leap second */ + { "20260228235959Z", 1772323199 }, + { "2026010124Z", 1767312000 }, /* 24:00 is the end of the day */ + { "20261231240000Z", 1798761600 }, /* ... so this is 2027-01-01 */ }; int