From 1c1b8f47ccec43e37de7ad2c6f5cc21866c2a765 Mon Sep 17 00:00:00 2001 From: vyassoham <211561779+vyassoham@users.noreply.github.com> Date: Thu, 19 Nov 2026 13:28:45 +0530 Subject: [PATCH 1/2] gh-160358: Validate year and day boundaries when unpickling date/datetime --- Lib/_pydatetime.py | 41 ++++--- Lib/test/datetimetester.py | 25 ++++ ...-11-19-13-25-00.gh-issue-160358.FixVal.rst | 1 + Modules/_datetimemodule.c | 112 ++++++++++++------ 4 files changed, 125 insertions(+), 54 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-19-13-25-00.gh-issue-160358.FixVal.rst diff --git a/Lib/_pydatetime.py b/Lib/_pydatetime.py index 5a8fec43e668a53..52137d280132f08 100644 --- a/Lib/_pydatetime.py +++ b/Lib/_pydatetime.py @@ -968,22 +968,25 @@ def __new__(cls, year, month=None, day=None): year, month, day (required, base 1) """ if (month is None and - isinstance(year, (bytes, str)) and len(year) == 4 and - 1 <= ord(year[2:3]) <= 12): + isinstance(year, (bytes, str)) and len(year) == 4): # Pickle support - if isinstance(year, str): + raw = year + if isinstance(raw, str): try: - year = year.encode('latin1') + raw = raw.encode('latin1') except UnicodeEncodeError: # More informative error message. raise ValueError( "Failed to encode latin1 string when unpickling " "a date object. " "pickle.load(data, encoding='latin1') is assumed.") - self = object.__new__(cls) - self.__setstate(year) - self._hashcode = -1 - return self + yhi, ylo, m, d = raw + y = yhi * 256 + ylo + if MINYEAR <= y <= MAXYEAR and 1 <= m <= 12 and 1 <= d <= _days_in_month(y, m): + self = object.__new__(cls) + self.__setstate(raw) + self._hashcode = -1 + return self year, month, day = _check_date_fields(year, month, day) self = object.__new__(cls) self._year = year @@ -1758,22 +1761,28 @@ class datetime(date): def __new__(cls, year, month=None, day=None, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0): - if (isinstance(year, (bytes, str)) and len(year) == 10 and - 1 <= ord(year[2:3])&0x7F <= 12): + if (isinstance(year, (bytes, str)) and len(year) == 10): # Pickle support - if isinstance(year, str): + raw = year + if isinstance(raw, str): try: - year = bytes(year, 'latin1') + raw = bytes(raw, 'latin1') except UnicodeEncodeError: # More informative error message. raise ValueError( "Failed to encode latin1 string when unpickling " "a datetime object. " "pickle.load(data, encoding='latin1') is assumed.") - self = object.__new__(cls) - self.__setstate(year, month) - self._hashcode = -1 - return self + yhi, ylo, m, d, hh, mm, ss, us1, us2, us3 = raw + y = yhi * 256 + ylo + m = m & 0x7F + us = (((us1 << 8) | us2) << 8) | us3 + if (MINYEAR <= y <= MAXYEAR and 1 <= m <= 12 and 1 <= d <= _days_in_month(y, m) + and 0 <= hh < 24 and 0 <= mm < 60 and 0 <= ss < 60 and 0 <= us < 1000000): + self = object.__new__(cls) + self.__setstate(raw, month) + self._hashcode = -1 + return self year, month, day = _check_date_fields(year, month, day) hour, minute, second, microsecond, fold = _check_time_fields( hour, minute, second, microsecond, fold) diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index 00a9d2bca3f4779..980bacce3de5afc 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -1387,6 +1387,31 @@ def test_repr_subclass(self): td = SubclassDate(2010, 10, day=10) self.assertEqual(repr(td), "SubclassDate(2010, 10, 10)") + def test_unpickle_invalid_state(self): + # gh-160358: Out-of-range year and day states must not create + # corrupted date/datetime objects or cause assertion failures. + is_date = (self.theclass is date or issubclass(self.theclass, date) and not issubclass(self.theclass, datetime)) + suffix = b"" if is_date else b"\x00\x00\x00\x00\x00\x00" + + invalid_states = [ + b'\x00\x00\x01\x01' + suffix, # year 0 + b'\x27\x10\x01\x01' + suffix, # year 10000 (MAXYEAR is 9999) + b'\x07\xe8\x01\x00' + suffix, # day 0 + b'\x07\xe8\x01\x20' + suffix, # day 32 in Jan + b'\x07\xe8\x02\x1e' + suffix, # day 30 in Feb (leap year 2024) + b'\x07\xe7\x02\x1d' + suffix, # day 29 in Feb (non-leap year 2023) + ] + if not is_date: + invalid_states.extend([ + b'\x07\xe8\x01\x01\x18\x00\x00\x00\x00\x00', # hour 24 + b'\x07\xe8\x01\x01\x00\x3c\x00\x00\x00\x00', # minute 60 + b'\x07\xe8\x01\x01\x00\x00\x3c\x00\x00\x00', # second 60 + b'\x07\xe8\x01\x01\x00\x00\x00\x0f\x42\x40', # microsecond 1000000 + ]) + for state in invalid_states: + with self.subTest(state=state): + self.assertRaises(TypeError, self.theclass, state) + def test_ordinal_conversions(self): # Check some fixed values. for y, m, d, n in [(1, 1, 1, 1), # calendar origin diff --git a/Misc/NEWS.d/next/Library/2026-11-19-13-25-00.gh-issue-160358.FixVal.rst b/Misc/NEWS.d/next/Library/2026-11-19-13-25-00.gh-issue-160358.FixVal.rst new file mode 100644 index 000000000000000..44fcebfdb1a1ab3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-19-13-25-00.gh-issue-160358.FixVal.rst @@ -0,0 +1 @@ +Fix process crash and assertion failure in :mod:`datetime` by validating year and day bounds when unpickling :class:`~datetime.date` and :class:`~datetime.datetime` objects. diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 091da059afd34b4..a70b64796a1b561 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -449,6 +449,14 @@ days_in_month(int year, int month) return _days_in_month[month]; } +static inline int +date_state_is_sane(int year, int month, int day) +{ + return (year >= MINYEAR && year <= MAXYEAR && + month >= 1 && month <= 12 && + day >= 1 && day <= days_in_month(year, month)); +} + /* year, month -> number of days in year preceding first day of month */ static int days_before_month(int year, int month) @@ -3260,30 +3268,37 @@ date_new(PyTypeObject *type, PyObject *args, PyObject *kw) if (PyTuple_GET_SIZE(args) == 1) { PyObject *state = PyTuple_GET_ITEM(args, 0); if (PyBytes_Check(state)) { - if (PyBytes_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE && - MONTH_IS_SANE(PyBytes_AS_STRING(state)[2])) - { - return date_from_pickle(type, state); + if (PyBytes_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE) { + const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(state); + int year = (data[0] << 8) | data[1]; + int month = data[2]; + int day = data[3]; + if (date_state_is_sane(year, month, day)) { + return date_from_pickle(type, state); + } } } else if (PyUnicode_Check(state)) { - if (PyUnicode_GET_LENGTH(state) == _PyDateTime_DATE_DATASIZE && - MONTH_IS_SANE(PyUnicode_READ_CHAR(state, 2))) - { - state = PyUnicode_AsLatin1String(state); - if (state == NULL) { - if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { - /* More informative error message. */ - PyErr_SetString(PyExc_ValueError, - "Failed to encode latin1 string when unpickling " - "a date object. " - "pickle.load(data, encoding='latin1') is assumed."); + if (PyUnicode_GET_LENGTH(state) == _PyDateTime_DATE_DATASIZE) { + int year = (PyUnicode_READ_CHAR(state, 0) << 8) | PyUnicode_READ_CHAR(state, 1); + int month = PyUnicode_READ_CHAR(state, 2); + int day = PyUnicode_READ_CHAR(state, 3); + if (date_state_is_sane(year, month, day)) { + state = PyUnicode_AsLatin1String(state); + if (state == NULL) { + if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { + /* More informative error message. */ + PyErr_SetString(PyExc_ValueError, + "Failed to encode latin1 string when unpickling " + "a date object. " + "pickle.load(data, encoding='latin1') is assumed."); + } + return NULL; } - return NULL; + PyObject *self = date_from_pickle(type, state); + Py_DECREF(state); + return self; } - PyObject *self = date_from_pickle(type, state); - Py_DECREF(state); - return self; } } } @@ -5570,30 +5585,51 @@ datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw) tzinfo = PyTuple_GET_ITEM(args, 1); } if (PyBytes_Check(state)) { - if (PyBytes_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE && - MONTH_IS_SANE(PyBytes_AS_STRING(state)[2] & 0x7F)) - { - return datetime_from_pickle(type, state, tzinfo); + if (PyBytes_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE) { + const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(state); + int year = (data[0] << 8) | data[1]; + int month = data[2] & 0x7F; + int day = data[3]; + int hour = data[4]; + int minute = data[5]; + int second = data[6]; + int usecond = (data[7] << 16) | (data[8] << 8) | data[9]; + if (date_state_is_sane(year, month, day) && + hour < 24 && minute < 60 && second < 60 && usecond < 1000000) + { + return datetime_from_pickle(type, state, tzinfo); + } } } else if (PyUnicode_Check(state)) { - if (PyUnicode_GET_LENGTH(state) == _PyDateTime_DATETIME_DATASIZE && - MONTH_IS_SANE(PyUnicode_READ_CHAR(state, 2) & 0x7F)) - { - state = PyUnicode_AsLatin1String(state); - if (state == NULL) { - if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { - /* More informative error message. */ - PyErr_SetString(PyExc_ValueError, - "Failed to encode latin1 string when unpickling " - "a datetime object. " - "pickle.load(data, encoding='latin1') is assumed."); + if (PyUnicode_GET_LENGTH(state) == _PyDateTime_DATETIME_DATASIZE) { + int year = (PyUnicode_READ_CHAR(state, 0) << 8) | PyUnicode_READ_CHAR(state, 1); + int month = PyUnicode_READ_CHAR(state, 2) & 0x7F; + int day = PyUnicode_READ_CHAR(state, 3); + int hour = PyUnicode_READ_CHAR(state, 4); + int minute = PyUnicode_READ_CHAR(state, 5); + int second = PyUnicode_READ_CHAR(state, 6); + int usecond = (PyUnicode_READ_CHAR(state, 7) << 16) | + (PyUnicode_READ_CHAR(state, 8) << 8) | + PyUnicode_READ_CHAR(state, 9); + if (date_state_is_sane(year, month, day) && + hour < 24 && minute < 60 && second < 60 && usecond < 1000000) + { + state = PyUnicode_AsLatin1String(state); + if (state == NULL) { + if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { + /* More informative error message. */ + PyErr_SetString(PyExc_ValueError, + "Failed to encode latin1 string when unpickling " + "a datetime object. " + "pickle.load(data, encoding='latin1') is assumed."); + } + return NULL; } - return NULL; + PyObject *self = datetime_from_pickle(type, state, tzinfo); + Py_DECREF(state); + return self; } - PyObject *self = datetime_from_pickle(type, state, tzinfo); - Py_DECREF(state); - return self; } } } From 669736f3ca5b9a0c56f3699a79c62eddb8dac07c Mon Sep 17 00:00:00 2001 From: vyassoham <211561779+vyassoham@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:41:38 +0530 Subject: [PATCH 2/2] gh-160358: Update test_backdoor_resistance to use sane year/day base state --- Lib/test/datetimetester.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index 980bacce3de5afc..830b3990e681c6a 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -2146,10 +2146,9 @@ def test_backdoor_resistance(self): # mistake) pass a string directly, which (if it's the right length) # will get treated like a pickle, and bypass the normal sanity # checks in the constructor. This can create insane objects. - # The constructor doesn't want to burn the time to validate all - # fields, but does check the month field. This stops, e.g., - # datetime.datetime('1995-03-25') from yielding an insane object. - base = b'1995-03-25' + # The constructor validates fields (year, month, day, time). + # This stops, e.g., datetime.datetime('1995-03-25') from yielding an insane object. + base = b'\x07\xe8\x01\x01\x00\x00\x00\x00\x00\x00' if not issubclass(self.theclass, datetime): base = base[:4] for month_byte in b'9', b'\0', b'\r', b'\xff': @@ -2161,11 +2160,13 @@ def test_backdoor_resistance(self): self.theclass(bytes([1] * len(base)), 'EST') for ord_byte in range(1, 13): - # This shouldn't blow up because of the month byte alone. If - # the implementation changes to do more-careful checking, it may - # blow up because other fields are insane. self.theclass(base[:2] + bytes([ord_byte]) + base[3:]) + # Arbitrary ASCII strings of matching length with out-of-range fields + # (e.g. '1995' or '1995-03-25') must be rejected as invalid states. + bad_ascii = b'1995-03-25' if issubclass(self.theclass, datetime) else b'1995' + self.assertRaises(TypeError, self.theclass, bad_ascii) + def test_valuerror_messages(self): pattern = re.compile( r"(year|month|day) must be in \d+\.\.\d+, not \d+"