From 5078bd943679a7d3120066dc41f26f65ab5c733d Mon Sep 17 00:00:00 2001 From: Demi Marie Obenour Date: Thu, 20 Apr 2023 16:40:16 -0400 Subject: [PATCH] Fix ordering of AdminVM and QubesVM AdminVM instances must always compare less than any QubesVM instance. Fixes: QubesOS/qubes-issues#6998 --- qubes/tests/vm/adminvm.py | 3 +++ qubes/tests/vm/qubesvm.py | 3 +++ qubes/vm/adminvm.py | 14 ++++++++++---- qubes/vm/qubesvm.py | 5 +++++ 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/qubes/tests/vm/adminvm.py b/qubes/tests/vm/adminvm.py index 52cc4f7ee..edbe9e210 100644 --- a/qubes/tests/vm/adminvm.py +++ b/qubes/tests/vm/adminvm.py @@ -180,3 +180,6 @@ def test_710_run_service_for_stdio(self, mock_run_service): self.assertEqual(exc.exception.returncode, 1) self.assertEqual(exc.exception.output, b'stdout') self.assertEqual(exc.exception.stderr, b'stderr') + + def test_711_adminvm_ordering(self): + assert(self.vm < qubes.vm.qubesvm.QubesVM(self.app, None, qid=1, name="dom0")) diff --git a/qubes/tests/vm/qubesvm.py b/qubes/tests/vm/qubesvm.py index 086d6088b..e98ad9ab1 100644 --- a/qubes/tests/vm/qubesvm.py +++ b/qubes/tests/vm/qubesvm.py @@ -2284,3 +2284,6 @@ def property_change(vm, property_name, new_value): property_change(test_vm, "template_for_dispvms", False) property_change(test_vm, "template_for_dispvms", True) + + def test_801_ordering(self): + assert qubes.vm.qubesvm.QubesVM(self.app, None, qid=1, name="bogus") > qubes.vm.adminvm.AdminVM(self.app, None) diff --git a/qubes/vm/adminvm.py b/qubes/vm/adminvm.py index d29bcd216..528715b52 100644 --- a/qubes/vm/adminvm.py +++ b/qubes/vm/adminvm.py @@ -29,9 +29,10 @@ import qubes.exc import qubes.vm from qubes.vm.qubesvm import _setter_kbd_layout +from qubes.vm import BaseVM -class AdminVM(qubes.vm.BaseVM): +class AdminVM(BaseVM): '''Dom0''' dir_path = None @@ -82,16 +83,21 @@ def __init__(self, *args, **kwargs): def __str__(self): return self.name - def __lt__(self, other): + def __lt__(self, other: object): + if not isinstance(other, BaseVM): + return NotImplemented # order dom0 before anything - return self.name != other.name + if not isinstance(other, AdminVM): + return True + assert self is other, "multiple instances of AdminVM?" + return False @property def attached_volumes(self): return [] @property - def xid(self): + def xid(self) -> int: '''Always ``0``. .. seealso: diff --git a/qubes/vm/qubesvm.py b/qubes/vm/qubesvm.py index 25caa7a1d..bc0a9cd08 100644 --- a/qubes/vm/qubesvm.py +++ b/qubes/vm/qubesvm.py @@ -40,6 +40,7 @@ import qubes.storage import qubes.utils import qubes.vm +import qubes.vm.adminvm import qubes.vm.mix.net qmemman_present = False @@ -959,6 +960,10 @@ def __hash__(self): return self.qid def __lt__(self, other): + if not isinstance(other, qubes.vm.BaseVM): + return NotImplemented + if isinstance(other, qubes.vm.adminvm.AdminVM): + return False return self.name < other.name def __xml__(self):