qmemman: fix units on meminfo parsing

meminfo (written by VM) is expected report KiB, but qmemman internally
use bytes. Convert units.
And also move obscure unit conversion in is_meminfo_suspicious to more
logical place in sanitize_and_parse_meminfo.
This commit is contained in:
Marek Marczykowski-Górecki 2017-06-21 06:34:00 +02:00
parent 8196b2d5bf
commit 588ff04f0d
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
2 changed files with 7 additions and 6 deletions

View File

@ -42,10 +42,11 @@ def sanitize_and_parse_meminfo(untrusted_meminfo):
# new syntax - just one int # new syntax - just one int
try: try:
if int(untrusted_meminfo) >= 0: if int(untrusted_meminfo) >= 0:
return int(untrusted_meminfo) return int(untrusted_meminfo) * 1024
except ValueError: except ValueError:
pass pass
untrusted_meminfo = untrusted_meminfo.decode('ascii', errors='strict')
# not new syntax - try the old one # not new syntax - try the old one
untrusted_dict = {} untrusted_dict = {}
# split meminfo contents into lines # split meminfo contents into lines
@ -61,9 +62,9 @@ def sanitize_and_parse_meminfo(untrusted_meminfo):
if not is_meminfo_suspicious(untrusted_dict): if not is_meminfo_suspicious(untrusted_dict):
# sanitize end # sanitize end
meminfo = untrusted_dict meminfo = untrusted_dict
return meminfo['MemTotal'] - \ return (meminfo['MemTotal'] -
meminfo['MemFree'] - meminfo['Cached'] - meminfo['Buffers'] + \ meminfo['MemFree'] - meminfo['Cached'] - meminfo['Buffers'] +
meminfo['SwapTotal'] - meminfo['SwapFree'] meminfo['SwapTotal'] - meminfo['SwapFree']) * 1024
return None return None
@ -78,7 +79,7 @@ def is_meminfo_suspicious(untrusted_meminfo):
try: try:
for i in ('MemTotal', 'MemFree', 'Buffers', 'Cached', for i in ('MemTotal', 'MemFree', 'Buffers', 'Cached',
'SwapTotal', 'SwapFree'): 'SwapTotal', 'SwapFree'):
val = int(untrusted_meminfo[i])*1024 val = int(untrusted_meminfo[i])
if val < 0: if val < 0:
ret = True ret = True
untrusted_meminfo[i] = val untrusted_meminfo[i] = val

View File

@ -137,7 +137,7 @@ class XS_Watcher(object):
self.log.debug('meminfo_changed(domain_id={!r})'.format(domain_id)) self.log.debug('meminfo_changed(domain_id={!r})'.format(domain_id))
untrusted_meminfo_key = self.handle.read( untrusted_meminfo_key = self.handle.read(
'', get_domain_meminfo_key(domain_id)) '', get_domain_meminfo_key(domain_id))
if untrusted_meminfo_key == None or untrusted_meminfo_key == '': if untrusted_meminfo_key == None or untrusted_meminfo_key == b'':
return return
self.log.debug('acquiring global_lock') self.log.debug('acquiring global_lock')