Repository navigation
Unbounded reads by zipfile may cause a MemoryError. #113977
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jan 12, 2024 That code already tries to check if the file is a zipfile by reading the header at the end of the file. The
MemoryErrorseems to indicate that seeking to the end of the file doesn't work as expected for/proc/kcore.This function could be written a bit more defensively though, for example by using
fpin.read(sizeEndCentDir+1)instead offpin.read().Reacted by sunmy2019 and Cody Maloney- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Jan 12, 2024 This implements the suggested change: #122101
Reacted by Cody MaloneyThere's a secondary thing here, that unbounded read defaults to the size at open with #120755. In this bug that's more than the amount of RAM remaining on the machine, hence the MemoryError / OOM. That stashed size is currently invalidated on
.truncate()but not on.seek()whichzipfileuses. I'd like to makeseek()clear the estimated size, but that change conflicts a lot with #121593, so have been holding off.#122101 I think resolves this case for
zipfile(and makes it more predictable, safer behavior generally) but I suspect the.seek()case will come up more in library code, so before python 3.14 would like to get that to invalidate the cached size generally.- changed the title
[-]When checking whether a file is a zip file, a MemoryError was triggered. After investigation, it was found that it was a read() read exception.[/-][+]Unbounded reads by `zipefile` may cause a `MemoryError`.[/+]on Nov 2, 2024 - added3.12only security fixesonly security fixes3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes
on Nov 2, 2024 Are (potential) DDoS like that considered as security issues or not? @gpshead
- changed the title
[-]Unbounded reads by `zipefile` may cause a `MemoryError`.[/-][+]Unbounded reads by `zipfile` may cause a `MemoryError`.[/+]on Nov 3, 2024 Are (potential) DDoS like that considered as security issues or not? @gpshead
They can be, but not particularly severe. I don't think this one is worth considering a security problem because the circumstances in which it can occur are extremely limited: Someone has to have opened a file that either isn't seekable or provides an egregious amount of data upon read after seeking to the end.
Just constructing that scenario in the first place is a sign of greater problems in the system.
@sethmlarson as FYI
Reacted by Cody MaloneyThanks for the nice bug report and PRs. The 3.12 and 3.13 back ports are also set to auto merge. Indeed, avoiding unbounded read assumptions is the right coding practice.
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Bug report
Bug description:
When checking whether a file is a zip file, MemoryError was triggered, followed by OOM. After investigation, it was found that it was a read() read exception.
Through PDB debugging, it was found that a link file was read, which points to /proc/kcore, why does the existing zip file check not determine whether it is a zip file by reading the header byte (504B0304) of the file .
I think the existing judgment ZIP method does not limit the read reading. When reading a non -normal file, it may cause the system to collapse .
Hope to be resolved.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs