Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions plugins/sudoers/gentime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions plugins/sudoers/regress/parser/check_gentime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down