mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 20:45:43 +00:00
fix(battlenet): repair install traversal before owner cleanup
This commit is contained in:
1 parent
0a7d0929b7
commit
01b45d0f08
2 files changed
+104
-37
No files matched your search
@@ -351,7 +351,7 @@ def install_path(root, library):
|
||||
|
||||
def _heal_install_permissions(folder):
|
||||
root = Path(folder)
|
||||
if root.is_symlink() or not root.exists():
|
||||
if any(path.is_symlink() for path in (root, *root.parents)) or not root.exists():
|
||||
return
|
||||
pending = [root]
|
||||
while pending:
|
||||
@@ -366,36 +366,39 @@ def _heal_install_permissions(folder):
|
||||
wanted = mode | stat.S_IRUSR | stat.S_IWUSR
|
||||
if stat.S_ISDIR(st.st_mode):
|
||||
wanted |= stat.S_IXUSR
|
||||
try:
|
||||
with os.scandir(path) as entries:
|
||||
for entry in entries:
|
||||
try:
|
||||
if entry.is_symlink():
|
||||
continue
|
||||
pending.append(Path(entry.path))
|
||||
except OSError:
|
||||
continue
|
||||
except OSError:
|
||||
pass
|
||||
if wanted != mode:
|
||||
try:
|
||||
os.chmod(path, wanted, follow_symlinks=False)
|
||||
except OSError:
|
||||
pass
|
||||
continue
|
||||
if stat.S_ISDIR(st.st_mode):
|
||||
try:
|
||||
with os.scandir(path) as entries:
|
||||
for entry in entries:
|
||||
try:
|
||||
if not entry.is_symlink():
|
||||
pending.append(Path(entry.path))
|
||||
except OSError:
|
||||
continue
|
||||
except OSError:
|
||||
continue
|
||||
|
||||
|
||||
def heal_install_folder(folder):
|
||||
folder = Path(folder).absolute()
|
||||
try:
|
||||
if any(path.is_symlink() for path in (folder, *folder.parents)):
|
||||
return
|
||||
_heal_install_permissions(folder)
|
||||
except (OSError, RuntimeError, ValueError):
|
||||
return
|
||||
try:
|
||||
game_env = runpy.run_path(os.path.join(os.path.dirname(os.path.abspath(__file__)), "droiddeck-game-env"))
|
||||
unlocked, cleared = game_env["heal_shared_data_locked"](Path(folder))
|
||||
unlocked, cleared = game_env["heal_shared_data_locked"](folder)
|
||||
except (OSError, KeyError, TypeError, ValueError):
|
||||
unlocked, cleared = [], []
|
||||
if cleared:
|
||||
say("cleared %d stale Windows owners in the game folder; unlocked: %s" % (len(cleared), ", ".join(unlocked) or "none"))
|
||||
try:
|
||||
_heal_install_permissions(folder)
|
||||
except (OSError, RuntimeError, ValueError):
|
||||
return
|
||||
|
||||
|
||||
def keeps_open(root):
|
||||
|
||||
@@ -5,6 +5,8 @@ from pathlib import Path
|
||||
import re
|
||||
import runpy
|
||||
import shutil
|
||||
import stat
|
||||
import struct
|
||||
import tempfile
|
||||
import time
|
||||
import unittest
|
||||
@@ -1509,32 +1511,86 @@ class InstallFolderHealTest(Temp):
|
||||
|
||||
with mock.patch.object(BNET["runpy"], "run_path", return_value={"heal_shared_data_locked": heal}), \
|
||||
mock.patch.dict(BNET, {"say": notes.append}):
|
||||
BNET["heal_install_folder"]("/Games/Battle.net/World of Warcraft")
|
||||
self.assertEqual(roots, [Path("/Games/Battle.net/World of Warcraft")])
|
||||
BNET["heal_install_folder"](str(self.tmp / "install"))
|
||||
self.assertEqual(roots, [self.tmp / "install"])
|
||||
self.assertEqual(notes, ["cleared 2 stale Windows owners in the game folder; unlocked: Data"])
|
||||
|
||||
def test_a_folder_that_cannot_be_checked_does_not_stop_the_install(self):
|
||||
for error in (OSError("xattrs"), KeyError("heal_shared_data_locked")):
|
||||
with self.subTest(error=error), mock.patch.object(BNET["runpy"], "run_path", side_effect=error):
|
||||
self.assertIsNone(BNET["heal_install_folder"]("/Games/Battle.net/World of Warcraft"))
|
||||
self.assertIsNone(BNET["heal_install_folder"](str(self.tmp / "install")))
|
||||
|
||||
def test_unreadable_install_folder_entries_are_made_writable(self):
|
||||
folder = self.tmp / "Games" / "Battle.net" / "World of Warcraft"
|
||||
folder = self.tmp / "install"
|
||||
data = folder / "Data"
|
||||
data.mkdir(parents=True)
|
||||
blocked = data / "data"
|
||||
blocked.write_bytes(b"x")
|
||||
os.chmod(blocked, 0o400)
|
||||
os.chmod(data, 0o500)
|
||||
os.chmod(folder, 0o500)
|
||||
with mock.patch.object(BNET["runpy"], "run_path", return_value={"heal_shared_data_locked": lambda root: ([], [])}):
|
||||
BNET["heal_install_folder"](str(folder))
|
||||
self.assertTrue(os.access(folder, os.W_OK | os.X_OK))
|
||||
self.assertTrue(os.access(data, os.W_OK | os.X_OK))
|
||||
self.assertTrue(os.access(blocked, os.W_OK))
|
||||
os.chmod(folder, 0o700)
|
||||
os.chmod(data, 0o700)
|
||||
os.chmod(blocked, 0o600)
|
||||
blocked.chmod(0o400)
|
||||
data.chmod(0o500)
|
||||
folder.chmod(0o500)
|
||||
try:
|
||||
with mock.patch.object(BNET["runpy"], "run_path", return_value={"heal_shared_data_locked": lambda root: ([], [])}):
|
||||
BNET["heal_install_folder"](str(folder))
|
||||
self.assertEqual(stat.S_IMODE(folder.stat().st_mode), 0o700)
|
||||
self.assertEqual(stat.S_IMODE(data.stat().st_mode), 0o700)
|
||||
self.assertEqual(stat.S_IMODE(blocked.stat().st_mode), 0o600)
|
||||
finally:
|
||||
folder.chmod(0o700)
|
||||
data.chmod(0o700)
|
||||
blocked.chmod(0o600)
|
||||
|
||||
def test_unreadable_descendants_are_repaired_before_windows_cleanup(self):
|
||||
folder = self.tmp / "install"
|
||||
data = folder / "Data"
|
||||
data.mkdir(parents=True)
|
||||
blocked = data / "payload"
|
||||
blocked.write_bytes(b"x")
|
||||
sibling = self.write("other/payload", "keep", 0o440)
|
||||
linked = self.write("external/payload", "keep", 0o440)
|
||||
(folder / "link").symlink_to(linked.parent, target_is_directory=True)
|
||||
subs = (21, 0x0badf00d, 0x0ddba11, 10122, 1000)
|
||||
sid = bytes([1, len(subs)]) + (5).to_bytes(6, "big") + struct.pack("<5I", *subs)
|
||||
descriptor = b"\x01\x00" + struct.pack("<BBHIIII", 1, 0, 0x8004, len(sid), 0, 0, 0) + sid
|
||||
try:
|
||||
os.setxattr(blocked, "user.wine.sd", descriptor)
|
||||
except OSError:
|
||||
self.skipTest("needs user xattrs")
|
||||
blocked.chmod(0)
|
||||
data.chmod(0o300)
|
||||
folder.chmod(0o2510)
|
||||
scan = os.scandir
|
||||
|
||||
def checked_scan(path):
|
||||
self.assertEqual(stat.S_IMODE(os.stat(path).st_mode) & 0o700, 0o700)
|
||||
return scan(path)
|
||||
|
||||
try:
|
||||
with mock.patch.object(BNET["os"], "scandir", side_effect=checked_scan):
|
||||
BNET["heal_install_folder"](str(folder))
|
||||
self.assertEqual(stat.S_IMODE(folder.stat().st_mode), 0o2710)
|
||||
self.assertEqual(stat.S_IMODE(data.stat().st_mode), 0o700)
|
||||
self.assertEqual(stat.S_IMODE(blocked.stat().st_mode), 0o600)
|
||||
self.assertNotIn("user.wine.sd", os.listxattr(blocked))
|
||||
self.assertEqual(stat.S_IMODE(sibling.stat().st_mode), 0o440)
|
||||
self.assertEqual(stat.S_IMODE(linked.stat().st_mode), 0o440)
|
||||
finally:
|
||||
folder.chmod(0o700)
|
||||
data.chmod(0o700)
|
||||
blocked.chmod(0o600)
|
||||
|
||||
def test_symlinked_roots_and_ancestors_are_not_repaired(self):
|
||||
target = self.tmp / "external"
|
||||
target.mkdir()
|
||||
payload = self.write("external/install/payload", "keep", 0o400)
|
||||
link = self.tmp / "link"
|
||||
link.symlink_to(target, target_is_directory=True)
|
||||
with mock.patch.object(BNET["runpy"], "run_path") as cleanup:
|
||||
for folder in (link, link / "install"):
|
||||
BNET["heal_install_folder"](str(folder))
|
||||
BNET["_heal_install_permissions"](str(folder))
|
||||
cleanup.assert_not_called()
|
||||
self.assertEqual(stat.S_IMODE(payload.stat().st_mode), 0o400)
|
||||
|
||||
|
||||
class AgentJobTest(Harness):
|
||||
@@ -1660,7 +1716,9 @@ class AgentJobTest(Harness):
|
||||
self.install()
|
||||
self.files = {".json": json.dumps({"progress": 1.0}),
|
||||
".game": json.dumps({"installed": True, "playable": True, "install_dir": "Z:/g", "local_version": "2"})}
|
||||
self.assertEqual(BNET["agent_job"](self.root, "update", "gryphon", poll=0), 0)
|
||||
with mock.patch.dict(BNET, {"heal_install_folder": mock.Mock()}) as values:
|
||||
self.assertEqual(BNET["agent_job"](self.root, "update", "gryphon", poll=0), 0)
|
||||
values["heal_install_folder"].assert_not_called()
|
||||
self.assertIn("agent-serve", self.started[0])
|
||||
self.assertEqual(json.loads(self.lines[-1][1])["version"], "2")
|
||||
|
||||
@@ -1811,7 +1869,9 @@ class AgentJobTest(Harness):
|
||||
def test_an_uninstall_is_done_once_the_agent_no_longer_lists_it(self):
|
||||
self.install()
|
||||
self.files = {".json": json.dumps({}), ".game": json.dumps({"installed": False})}
|
||||
self.assertEqual(BNET["agent_job"](self.root, "uninstall", "gryphon", poll=0), 0)
|
||||
with mock.patch.dict(BNET, {"heal_install_folder": mock.Mock()}) as values:
|
||||
self.assertEqual(BNET["agent_job"](self.root, "uninstall", "gryphon", poll=0), 0)
|
||||
values["heal_install_folder"].assert_not_called()
|
||||
self.assertEqual(self.lines[-1], ("DONE", json.dumps({"uninstalled": "gryphon"})))
|
||||
|
||||
def test_a_gamescope_that_cannot_start_is_reported(self):
|
||||
@@ -1847,7 +1907,9 @@ class AgentJobTest(Harness):
|
||||
self.assertEqual(BNET["agent_job"](self.root, "uninstall", "gryphon", poll=0), 1)
|
||||
self.assertEqual(self.lines[-1][0], "ERROR")
|
||||
self.files = {".game": json.dumps({})}
|
||||
self.assertEqual(BNET["agent_job"](self.root, "uninstall", "gryphon", poll=0), 0)
|
||||
with mock.patch.dict(BNET, {"heal_install_folder": mock.Mock()}) as values:
|
||||
self.assertEqual(BNET["agent_job"](self.root, "uninstall", "gryphon", poll=0), 0)
|
||||
values["heal_install_folder"].assert_not_called()
|
||||
self.assertEqual(self.lines[-1], ("DONE", json.dumps({"uninstalled": "gryphon"})))
|
||||
|
||||
def test_without_battle_net_nothing_starts(self):
|
||||
@@ -1859,7 +1921,9 @@ class AgentJobTest(Harness):
|
||||
self.install()
|
||||
self.states = [self.clients() + [(40, self.GAME)], [(40, self.GAME)], []]
|
||||
self.files = {".game": json.dumps({"installed": False})}
|
||||
self.assertEqual(BNET["agent_job"](self.root, "uninstall", "gryphon", poll=0), 0)
|
||||
with mock.patch.dict(BNET, {"heal_install_folder": mock.Mock()}) as values:
|
||||
self.assertEqual(BNET["agent_job"](self.root, "uninstall", "gryphon", poll=0), 0)
|
||||
values["heal_install_folder"].assert_not_called()
|
||||
self.assertEqual(self.lines[0], ("STATE", "waiting"))
|
||||
self.assertEqual(self.lines[1], ("STATE", "starting"))
|
||||
|
||||
|
||||
Reference in new issue
Block a user