Skip to content

Commit

Permalink
Verify if service names are not too long
Browse files Browse the repository at this point in the history
Reject them before actually setting them. But if there is already some
too-long set, ignore it with a warning instead of breaking VM startup.

And also remove test for too strict verification.
  • Loading branch information
marmarek committed May 31, 2024
1 parent 0ec452e commit cf8a863
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
21 changes: 21 additions & 0 deletions qubes/ext/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ def on_domain_qdb_create(self, vm, event):
if not feature.startswith('service.'):
continue
service = feature[len('service.'):]
if not service:
vm.log.warning("Empty service name, ignoring: " + service)
continue

Check warning on line 67 in qubes/ext/services.py

View check run for this annotation

Codecov / codecov/patch

qubes/ext/services.py#L66-L67

Added lines #L66 - L67 were not covered by tests
if len(service) > 48:
vm.log.warning("Too long service name, ignoring: " + service)
continue

Check warning on line 70 in qubes/ext/services.py

View check run for this annotation

Codecov / codecov/patch

qubes/ext/services.py#L69-L70

Added lines #L69 - L70 were not covered by tests
# forcefully convert to '0' or '1'
vm.untrusted_qdb.write('/qubes-service/{}'.format(service),
str(int(bool(value))))
Expand All @@ -70,6 +76,21 @@ def on_domain_qdb_create(self, vm, event):
vm.untrusted_qdb.write('/qubes-service/meminfo-writer',
'1' if vm.maxmem > 0 else '0')

@qubes.ext.handler('domain-feature-pre-set:*')
def on_domain_feature_pre_set(self, vm, event, feature,
value, oldvalue=None):
"""Check if service name is compatible with QubesDB"""
# pylint: disable=unused-argument
if not feature.startswith('service.'):
return
service = feature[len('service.'):]
if not service:
raise qubes.exc.QubesValueError(
'Service name cannot be empty')
if len(service) > 48:
raise qubes.exc.QubesValueError(
'Service name must not exceed 48 bytes')

@qubes.ext.handler('domain-feature-set:*')
def on_domain_feature_set(self, vm, event, feature, value, oldvalue=None):
"""Update /qubes-service/ QubesDB tree in runtime"""
Expand Down
7 changes: 0 additions & 7 deletions qubes/tests/api_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1403,13 +1403,6 @@ def test_323_feature_set_service_too_long(self):
self.assertNotIn('test-feature', self.vm.features)
self.assertFalse(self.app.save.called)

def test_324_feature_set_service_bad_name(self):
with self.assertRaises(qubes.exc.QubesValueError):
self.call_mgmt_func(b'admin.vm.feature.Set',
b'test-vm1', b'service.0')
self.assertNotIn('test-feature', self.vm.features)
self.assertFalse(self.app.save.called)

def test_325_feature_set_service_empty_name(self):
with self.assertRaises(qubes.exc.QubesValueError):
self.call_mgmt_func(b'admin.vm.feature.Set',
Expand Down

0 comments on commit cf8a863

Please sign in to comment.