Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*.zip binary

# Specific binary files
Lib/test/test_zoneinfo/data/tzif_* binary
Comment thread
StanFromIreland marked this conversation as resolved.
Outdated
PC/classicAppCompat.* binary

# Text files that should not be subject to eol conversion
Expand Down
Binary file added Lib/test/test_zoneinfo/data/tzif_invalid_lookahead
Binary file not shown.
Binary file added Lib/test/test_zoneinfo/data/tzif_invalid_trans_idx
Binary file not shown.
10 changes: 10 additions & 0 deletions Lib/test/test_zoneinfo/test_zoneinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,16 @@ def test_empty_zone(self):
with self.assertRaises(ValueError):
self.klass.from_file(zf)

def test_invalid_transition_index(self):
Comment thread
StanFromIreland marked this conversation as resolved.
with open(DATA_DIR / "tzif_invalid_trans_idx", "rb") as f:
with self.assertRaises(ValueError):
self.klass.from_file(f)

def test_transition_lookahead_out_of_bounds(self):
with open(DATA_DIR / "tzif_invalid_lookahead", "rb") as f:
zi = self.klass.from_file(f)
self.assertIsNotNone(zi)

def test_zone_very_large_timestamp(self):
"""Test when a transition is in the far past or future.

Expand Down
4 changes: 4 additions & 0 deletions Lib/zoneinfo/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ def load_data(fobj):
trans_list_utc = ()
trans_idx = ()

for idx in trans_idx:
Comment thread
StanFromIreland marked this conversation as resolved.
Outdated
if idx >= typecnt:
Comment thread
StanFromIreland marked this conversation as resolved.
Outdated
raise ValueError(f"Invalid transition index found while reading TZif: {idx}")

# Read the ttinfo struct, (utoff, isdst, abbrind)
if typecnt:
utcoff, isdst, abbrind = zip(
Expand Down
2 changes: 1 addition & 1 deletion Lib/zoneinfo/_zoneinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def _utcoff_to_dstoff(trans_idx, utcoffsets, isdsts):
if not isdsts[comp_idx]:
dstoff = utcoff - utcoffsets[comp_idx]

if not dstoff and idx < (typecnt - 1):
if not dstoff and idx < (typecnt - 1) and i + 1 < len(trans_idx):
comp_idx = trans_idx[i + 1]

# If the following transition is also DST and we couldn't
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`zoneinfo`: Fix heap buffer overflow reads from malformed TZif data.
Found by OSS Fuzz, issues :oss-fuzz:`492245058` and :oss-fuzz:`492230068`.
4 changes: 2 additions & 2 deletions Modules/_zoneinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,7 @@ load_data(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self, PyObject *file_obj)
}

trans_idx[i] = (size_t)cur_trans_idx;
if (trans_idx[i] > self->num_ttinfos) {
if (trans_idx[i] >= self->num_ttinfos) {
PyErr_Format(
PyExc_ValueError,
"Invalid transition index found while reading TZif: %zd",
Expand Down Expand Up @@ -2063,7 +2063,7 @@ utcoff_to_dstoff(size_t *trans_idx, long *utcoffs, long *dstoffs,
dstoff = utcoff - utcoffs[comp_idx];
}

if (!dstoff && idx < (num_ttinfos - 1)) {
if (!dstoff && idx < (num_ttinfos - 1) && i + 1 < num_transitions) {
Comment thread
serhiy-storchaka marked this conversation as resolved.
comp_idx = trans_idx[i + 1];

// If the following transition is also DST and we couldn't find
Expand Down
3 changes: 3 additions & 0 deletions Tools/build/compute-changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@
Path("Modules/pyexpat.c"),
# zipfile
Path("Lib/zipfile/"),
# zoneinfo
Path("Lib/zoneinfo/"),
Path("Modules/_zoneinfo.c"),
Comment on lines +102 to +104
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is it about? How is this list use and why these additions are necessary?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the list of files that triggers CIFuzz, we started fuzzing zoneinfo (which found one of the two bugs the following day) in python/library-fuzzers@eb273d5. It's not related to the issue, but it's so small I think including it here is fine.

})


Expand Down
Loading