Skip to content

Commit f3b459e

Browse files
kk7dsJohnGarbutt
authored andcommitted
[stable-only][cve] Check VMDK create-type against an allowed list
NOTE(sbauza): Stable policy allows us to proactively merge a backport without waiting for the parent patch to be merged (exception to rule #4 in [1]. Marking [stable-only] in order to silence nova-tox-validate-backport [1] https://docs.openstack.org/project-team-guide/stable-branches.html#appropriate-fixes Conflicts vs victoria in: nova/conf/compute.py Related-Bug: #1996188 Change-Id: I5a399f1d3d702bfb76c067893e9c924904c8c360 (cherry picked from commit bf8b6f698c23c631c0a71d77f2716ca292c45e74)
1 parent 1bf98f1 commit f3b459e

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

nova/conf/compute.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,15 @@
959959
* Any integer >= 1 represents the maximum allowed. A value of 0 will cause the
960960
``nova-compute`` service to fail to start, as 0 disk devices is an invalid
961961
configuration that would prevent instances from being able to boot.
962+
"""),
963+
cfg.ListOpt('vmdk_allowed_types',
964+
default=['streamOptimized', 'monolithicSparse'],
965+
help="""
966+
A list of strings describing allowed VMDK "create-type" subformats
967+
that will be allowed. This is recommended to only include
968+
single-file-with-sparse-header variants to avoid potential host file
969+
exposure due to processing named extents. If this list is empty, then no
970+
form of VMDK image will be allowed.
962971
"""),
963972
]
964973

nova/tests/unit/virt/test_images.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import mock
1818
from oslo_concurrency import processutils
19+
from oslo_serialization import jsonutils
20+
from oslo_utils import imageutils
1921
import six
2022

2123
from nova.compute import utils as compute_utils
@@ -136,3 +138,47 @@ def test_convert_image_without_direct_io_support(self, mock_execute,
136138
'-O', 'out_format', '-f', 'in_format', 'source', 'dest')
137139
mock_disk_op_sema.__enter__.assert_called_once()
138140
self.assertTupleEqual(expected, mock_execute.call_args[0])
141+
142+
def test_convert_image_vmdk_allowed_list_checking(self):
143+
info = {'format': 'vmdk',
144+
'format-specific': {
145+
'type': 'vmdk',
146+
'data': {
147+
'create-type': 'monolithicFlat',
148+
}}}
149+
150+
# If the format is not in the allowed list, we should get an error
151+
self.assertRaises(exception.ImageUnacceptable,
152+
images.check_vmdk_image, 'foo',
153+
imageutils.QemuImgInfo(jsonutils.dumps(info),
154+
format='json'))
155+
156+
# With the format in the allowed list, no error
157+
self.flags(vmdk_allowed_types=['streamOptimized', 'monolithicFlat',
158+
'monolithicSparse'],
159+
group='compute')
160+
images.check_vmdk_image('foo',
161+
imageutils.QemuImgInfo(jsonutils.dumps(info),
162+
format='json'))
163+
164+
# With an empty list, allow nothing
165+
self.flags(vmdk_allowed_types=[], group='compute')
166+
self.assertRaises(exception.ImageUnacceptable,
167+
images.check_vmdk_image, 'foo',
168+
imageutils.QemuImgInfo(jsonutils.dumps(info),
169+
format='json'))
170+
171+
@mock.patch.object(images, 'fetch')
172+
@mock.patch('nova.privsep.qemu.unprivileged_qemu_img_info')
173+
def test_fetch_checks_vmdk_rules(self, mock_info, mock_fetch):
174+
info = {'format': 'vmdk',
175+
'format-specific': {
176+
'type': 'vmdk',
177+
'data': {
178+
'create-type': 'monolithicFlat',
179+
}}}
180+
mock_info.return_value = jsonutils.dumps(info)
181+
with mock.patch('os.path.exists', return_value=True):
182+
e = self.assertRaises(exception.ImageUnacceptable,
183+
images.fetch_to_raw, None, 'foo', 'anypath')
184+
self.assertIn('Invalid VMDK create-type specified', str(e))

nova/virt/images.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,34 @@ def get_info(context, image_href):
110110
return IMAGE_API.get(context, image_href)
111111

112112

113+
def check_vmdk_image(image_id, data):
114+
# Check some rules about VMDK files. Specifically we want to make
115+
# sure that the "create-type" of the image is one that we allow.
116+
# Some types of VMDK files can reference files outside the disk
117+
# image and we do not want to allow those for obvious reasons.
118+
119+
types = CONF.compute.vmdk_allowed_types
120+
121+
if not len(types):
122+
LOG.warning('Refusing to allow VMDK image as vmdk_allowed_'
123+
'types is empty')
124+
msg = _('Invalid VMDK create-type specified')
125+
raise exception.ImageUnacceptable(image_id=image_id, reason=msg)
126+
127+
try:
128+
create_type = data.format_specific['data']['create-type']
129+
except KeyError:
130+
msg = _('Unable to determine VMDK create-type')
131+
raise exception.ImageUnacceptable(image_id=image_id, reason=msg)
132+
133+
if create_type not in CONF.compute.vmdk_allowed_types:
134+
LOG.warning('Refusing to process VMDK file with create-type of %r '
135+
'which is not in allowed set of: %s', create_type,
136+
','.join(CONF.compute.vmdk_allowed_types))
137+
msg = _('Invalid VMDK create-type specified')
138+
raise exception.ImageUnacceptable(image_id=image_id, reason=msg)
139+
140+
113141
def fetch_to_raw(context, image_href, path, trusted_certs=None):
114142
path_tmp = "%s.part" % path
115143
fetch(context, image_href, path_tmp, trusted_certs)
@@ -129,6 +157,9 @@ def fetch_to_raw(context, image_href, path, trusted_certs=None):
129157
reason=(_("fmt=%(fmt)s backed by: %(backing_file)s") %
130158
{'fmt': fmt, 'backing_file': backing_file}))
131159

160+
if fmt == 'vmdk':
161+
check_vmdk_image(image_href, data)
162+
132163
if fmt != "raw" and CONF.force_raw_images:
133164
staged = "%s.converted" % path
134165
LOG.debug("%s was %s, converting to raw", image_href, fmt)

0 commit comments

Comments
 (0)