Skip to content

Commit

Permalink
Add bounds check to mmap reads
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Davy <simon.davy@canonical.com>
  • Loading branch information
Simon Davy committed Oct 18, 2018
1 parent 3088bc1 commit f9d6607
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions prometheus_client/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,10 @@ def _read_all_values(self):

while pos < used:
encoded_len = _unpack_integer(data, pos)[0]
# check we are not reading beyond bounds
if encoded_len + pos > used:
msg = 'Read beyond file size detected, %s is corrupted.'
raise RuntimeError(msg % self._fname)
pos += 4
encoded = unpack_from(('%ss' % encoded_len).encode(), data, pos)[0]
padded_len = encoded_len + (8 - (encoded_len + 4) % 8)
Expand Down
7 changes: 7 additions & 0 deletions tests/test_multiprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,13 @@ def test_multi_expansion(self):
[('abc', 42.0), (key, 123.0), ('def', 17.0)],
list(self.d.read_all_values()))

def test_corruption_detected(self):
self.d.write_value('abc', 42.0)
# corrupt the written data
self.d._m[8:16] = b'somejunk'
with self.assertRaises(RuntimeError):
list(self.d.read_all_values())

def tearDown(self):
os.unlink(self.tempfile)

Expand Down

0 comments on commit f9d6607

Please sign in to comment.