Skip to content

Commit 883fc8a

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Enforce image safety during image_conversion" into stable/wallaby
2 parents 2940ab9 + 9a98c4a commit 883fc8a

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

glance/async_/flows/plugins/image_conversion.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,29 @@ def _execute(self, action, file_path, **kwargs):
116116
virtual_size = metadata.get('virtual-size', 0)
117117
action.set_image_attribute(virtual_size=virtual_size)
118118

119+
if 'backing-filename' in metadata:
120+
LOG.warning('Refusing to process QCOW image with a backing file')
121+
raise RuntimeError(
122+
'QCOW images with backing files are not allowed')
123+
124+
if metadata.get('format') == 'vmdk':
125+
create_type = metadata.get(
126+
'format-specific', {}).get(
127+
'data', {}).get('create-type')
128+
allowed = CONF.image_format.vmdk_allowed_types
129+
if not create_type:
130+
raise RuntimeError(_('Unable to determine VMDK create-type'))
131+
if not len(allowed):
132+
LOG.warning(_('Refusing to process VMDK file as '
133+
'vmdk_allowed_types is empty'))
134+
raise RuntimeError(_('Image is a VMDK, but no VMDK createType '
135+
'is specified'))
136+
if create_type not in allowed:
137+
LOG.warning(_('Refusing to process VMDK file with create-type '
138+
'of %r which is not in allowed set of: %s'),
139+
create_type, ','.join(allowed))
140+
raise RuntimeError(_('Invalid VMDK create-type specified'))
141+
119142
if source_format == target_format:
120143
LOG.debug("Source is already in target format, "
121144
"not doing conversion for %s", self.image_id)

glance/common/config.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@
100100
"image attribute"),
101101
deprecated_opts=[cfg.DeprecatedOpt('disk_formats',
102102
group='DEFAULT')]),
103+
cfg.ListOpt('vmdk_allowed_types',
104+
default=['streamOptimized', 'monolithicSparse'],
105+
help=_("A list of strings describing allowed VMDK "
106+
"'create-type' subformats that will be allowed. "
107+
"This is recommended to only include "
108+
"single-file-with-sparse-header variants to avoid "
109+
"potential host file exposure due to processing named "
110+
"extents. If this list is empty, then no VDMK image "
111+
"types allowed. Note that this is currently only "
112+
"checked during image conversion (if enabled), and "
113+
"limits the types of VMDK images we will convert "
114+
"from.")),
103115
]
104116
task_opts = [
105117
cfg.IntOpt('task_time_to_live',

glance/tests/unit/async_/flows/plugins/test_image_conversion.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,53 @@ def test_image_convert_inspection_reports_error(self):
169169
# Make sure we did not update the image
170170
self.img_repo.save.assert_not_called()
171171

172+
def test_image_convert_invalid_qcow(self):
173+
data = {'format': 'qcow2',
174+
'backing-filename': '/etc/hosts'}
175+
176+
convert = self._setup_image_convert_info_fail()
177+
with mock.patch.object(processutils, 'execute') as exc_mock:
178+
exc_mock.return_value = json.dumps(data), ''
179+
e = self.assertRaises(RuntimeError,
180+
convert.execute, 'file:///test/path.qcow')
181+
self.assertEqual('QCOW images with backing files are not allowed',
182+
str(e))
183+
184+
def _test_image_convert_invalid_vmdk(self):
185+
data = {'format': 'vmdk',
186+
'format-specific': {
187+
'data': {
188+
'create-type': 'monolithicFlat',
189+
}}}
190+
191+
convert = self._setup_image_convert_info_fail()
192+
with mock.patch.object(processutils, 'execute') as exc_mock:
193+
exc_mock.return_value = json.dumps(data), ''
194+
convert.execute('file:///test/path.vmdk')
195+
196+
def test_image_convert_invalid_vmdk(self):
197+
e = self.assertRaises(RuntimeError,
198+
self._test_image_convert_invalid_vmdk)
199+
self.assertEqual('Invalid VMDK create-type specified', str(e))
200+
201+
def test_image_convert_valid_vmdk_no_types(self):
202+
with mock.patch.object(CONF.image_format, 'vmdk_allowed_types',
203+
new=[]):
204+
# We make it past the VMDK check and fail because our file
205+
# does not exist
206+
e = self.assertRaises(RuntimeError,
207+
self._test_image_convert_invalid_vmdk)
208+
self.assertEqual('Image is a VMDK, but no VMDK createType is '
209+
'specified', str(e))
210+
211+
def test_image_convert_valid_vmdk(self):
212+
with mock.patch.object(CONF.image_format, 'vmdk_allowed_types',
213+
new=['monolithicSparse', 'monolithicFlat']):
214+
# We make it past the VMDK check and fail because our file
215+
# does not exist
216+
self.assertRaises(FileNotFoundError,
217+
self._test_image_convert_invalid_vmdk)
218+
172219
def test_image_convert_fails(self):
173220
convert = self._setup_image_convert_info_fail()
174221
with mock.patch.object(processutils, 'execute') as exc_mock:

0 commit comments

Comments
 (0)