From 2bb73ab0a1b5128184564cff36ead530446553f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Sat, 21 May 2016 03:51:21 +0200 Subject: [PATCH] qubes/vm: add validators for many properties --- qubes/vm/mix/net.py | 10 ++++++++++ qubes/vm/qubesvm.py | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/qubes/vm/mix/net.py b/qubes/vm/mix/net.py index f26e79a1..f0899d29 100644 --- a/qubes/vm/mix/net.py +++ b/qubes/vm/mix/net.py @@ -26,14 +26,24 @@ import libvirt import lxml.etree +import re import qubes import qubes.events import qubes.exc +def _setter_mac(self, prop, value): + if not isinstance(value, basestring): + raise ValueError('MAC address must be a string') + value = value.lower() + if re.match(r"^([0-9a-f][0-9a-f]:){5}[0-9a-f][0-9a-f]$", value) is None: + raise ValueError('Invalid MAC address value') + return value + class NetVMMixin(qubes.events.Emitter): mac = qubes.property('mac', type=str, default='00:16:3E:5E:6C:00', + setter=_setter_mac, ls_width=17, doc='MAC address of the NIC emulated inside VM') diff --git a/qubes/vm/qubesvm.py b/qubes/vm/qubesvm.py index 043fe0ce..fc6aac0b 100644 --- a/qubes/vm/qubesvm.py +++ b/qubes/vm/qubesvm.py @@ -93,6 +93,10 @@ def _setter_name(self, prop, value): except AttributeError: pass + if value in self.app.domains: + raise qubes.exc.QubesException( + 'VM named {} alread exists'.format(value)) + return value @@ -125,6 +129,12 @@ def _setter_label(self, prop, value): return self.app.get_label(value) +def _setter_positive_int(self, prop, value): + value = int(value) + if value <= 0: + raise ValueError('Value must be positive') + return value + class QubesVM(qubes.vm.mix.net.NetVMMixin, qubes.vm.BaseVM): '''Base functionality of Qubes VM shared between all VMs.''' @@ -185,11 +195,13 @@ class QubesVM(qubes.vm.mix.net.NetVMMixin, qubes.vm.BaseVM): package manager.''') memory = qubes.property('memory', type=int, + setter=_setter_positive_int, default=(lambda self: qubes.config.defaults['hvm_memory' if self.hvm else 'memory']), doc='Memory currently available for this VM.') maxmem = qubes.property('maxmem', type=int, + setter=_setter_positive_int, default=(lambda self: self.app.host.memory_total / 1024 / 2), doc='''Maximum amount of memory available for this VM (for the purpose of the memory balancer).''') @@ -202,6 +214,7 @@ class QubesVM(qubes.vm.mix.net.NetVMMixin, qubes.vm.BaseVM): # FIXME self.app.host could not exist - only self.app.vm required by API vcpus = qubes.property('vcpus', type=int, + setter=_setter_positive_int, default=(lambda self: self.app.host.no_cpus), ls_width=2, doc='FIXME') @@ -252,6 +265,7 @@ class QubesVM(qubes.vm.mix.net.NetVMMixin, qubes.vm.BaseVM): # return self._default_user qrexec_timeout = qubes.property('qrexec_timeout', type=int, default=60, + setter=_setter_positive_int, ls_width=3, doc='''Time in seconds after which qrexec connection attempt is deemed failed. Operating system inside VM should be able to boot in this