Skip to content

Commit 0a93398

Browse files
committed
Prevent backing up dom0 to itself (in home dir)
resolves: QubesOS/qubes-issues#10127
1 parent a930d4e commit 0a93398

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

qubes/backup.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import itertools
2929
import logging
3030
import os
31+
from pathlib import Path
3132
import pwd
3233
import shutil
3334
import stat
@@ -452,6 +453,15 @@ def get_files_to_backup(self):
452453
if 0 in [vm.qid for vm in self.vms_for_backup]:
453454
local_user = grp.getgrnam("qubes").gr_mem[0]
454455
home_dir = pwd.getpwnam(local_user).pw_dir
456+
457+
# Checking if target is not user home directory in dom0
458+
if self.target_dir in ["", "~"] or Path(
459+
self.target_dir
460+
).is_relative_to(home_dir):
461+
raise qubes.exc.QubesException(
462+
"Can not backup dom0 home directory to itself!"
463+
)
464+
455465
# Home dir should have only user-owned files, so fix it now
456466
# to prevent permissions problems - some root-owned files can
457467
# left after 'sudo bash' and similar commands

qubes/tests/api_admin.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3535,6 +3535,32 @@ def test_602_backup_info_profile_missing_destination_vm(self):
35353535
b"admin.backup.Info", b"dom0", b"testprofile"
35363536
)
35373537

3538+
def test_603_backup_info_dom0_to_dom0_home(self):
3539+
if self.local_name == "dom0":
3540+
# Test is performed at dom0. Use exact homedir location
3541+
home_dir = os.environ["HOME"]
3542+
else:
3543+
home_dir = "~"
3544+
backup_profile = (
3545+
"include:\n"
3546+
" - dom0\n"
3547+
"destination_vm: dom0\n"
3548+
"destination_path: " + home_dir +"\n"
3549+
"passphrase_text: test\n"
3550+
)
3551+
with tempfile.TemporaryDirectory() as profile_dir:
3552+
with open(
3553+
os.path.join(profile_dir, "testprofile.conf"), "w"
3554+
) as profile_file:
3555+
profile_file.write(backup_profile)
3556+
with unittest.mock.patch(
3557+
"qubes.config.backup_profile_dir", profile_dir
3558+
):
3559+
with self.assertRaises(qubes.exc.QubesException):
3560+
self.call_mgmt_func(
3561+
b"admin.backup.Info", b"dom0", b"testprofile"
3562+
)
3563+
35383564
def test_610_backup_cancel_not_running(self):
35393565
with self.assertRaises(qubes.exc.QubesException):
35403566
self.call_mgmt_func(b"admin.backup.Cancel", b"dom0", b"testprofile")

qubes/tests/integ/backup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,9 @@ def make_backup(self, vms, target=None, expect_failure=False, **kwargs):
198198
if target is None:
199199
target = self.backupdir
200200
try:
201-
backup = qubes.backup.Backup(self.app, vms, **kwargs)
201+
backup = qubes.backup.Backup(
202+
self.app, vms, target_dir=target, **kwargs
203+
)
202204
except qubes.exc.QubesException as e:
203205
if not expect_failure:
204206
self.fail("QubesException during backup_prepare: %s" % str(e))
@@ -207,7 +209,6 @@ def make_backup(self, vms, target=None, expect_failure=False, **kwargs):
207209

208210
if "passphrase" not in kwargs:
209211
backup.passphrase = "qubes"
210-
backup.target_dir = target
211212

212213
try:
213214
self.loop.run_until_complete(backup.backup_do())

0 commit comments

Comments
 (0)