mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-25 15:56:01 +00:00
fix(runtime): programs can open ProgramData folders a stale Windows owner locked (#3194)
* Unlock ProgramData entries a stale Windows owner locked Wine builds the user SID from /etc/machine-id and the uid it runs as. When either changes, descriptors stored in user.wine.sd keep the old owner, and the next DACL change maps to mode 000 because no ACE matches the token. Wine then cannot open the entry, so programs such as Battle.net cannot repair their own folders. Before each launch, clear stale local user owners under ProgramData and give owner access back to entries they locked. * Heal a locked ProgramData itself and keep read-only folders * Serialise the ProgramData heal per prefix * Launch even when the ProgramData owner check fails * Read the machine ID as bytes the way Wine does * Parse a signed machine ID the way Wine does
This commit is contained in:
1 parent
a6fe10d8ff
commit
d38fd373f0
2 files changed
+290
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import contextlib
|
||||
import fcntl
|
||||
import hashlib
|
||||
import io
|
||||
import json
|
||||
@@ -7,9 +8,11 @@ import shutil
|
||||
from pathlib import Path
|
||||
import runpy
|
||||
import shlex
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
from unittest import mock
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
@@ -3727,6 +3730,161 @@ class ForeignSystemDllTest(unittest.TestCase):
|
||||
self.assertEqual([], self.run_repair(Path(tmp) / "missing", proton))
|
||||
|
||||
|
||||
class SharedDataHealTest(unittest.TestCase):
|
||||
CURRENT = (21, 0x1234abcd, 0x5678ef01, os.getuid(), 1000)
|
||||
STALE = (21, 0x0badf00d, 0x0ddba11, 10122, 1000)
|
||||
|
||||
def setUp(self):
|
||||
self.globals = MODULE["heal_shared_data"].__globals__
|
||||
self.old_machine_id = self.globals["MACHINE_ID"]
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.cleanup)
|
||||
machine_id = Path(self.tmp.name) / "machine-id"
|
||||
machine_id.write_text("1234abcd5678ef01%s\n" % ("0" * 16))
|
||||
self.globals["MACHINE_ID"] = str(machine_id)
|
||||
self.compat = Path(self.tmp.name) / "compat"
|
||||
self.root = self.compat / "pfx/drive_c/ProgramData"
|
||||
self.root.mkdir(parents=True)
|
||||
probe = self.root / "probe"
|
||||
probe.write_text("")
|
||||
try:
|
||||
os.setxattr(probe, "user.wine.sd", b"x")
|
||||
except OSError:
|
||||
self.skipTest("needs user xattrs")
|
||||
probe.unlink()
|
||||
|
||||
def cleanup(self):
|
||||
self.globals["MACHINE_ID"] = self.old_machine_id
|
||||
for folder, dirs, files in os.walk(self.tmp.name):
|
||||
for name in dirs + files:
|
||||
path = os.path.join(folder, name)
|
||||
if not os.path.islink(path):
|
||||
os.chmod(path, 0o700)
|
||||
self.tmp.cleanup()
|
||||
|
||||
def own(self, path, subs, authority=5):
|
||||
sid = bytes([1, len(subs)]) + authority.to_bytes(6, "big") + struct.pack("<%dI" % len(subs), *subs)
|
||||
header = struct.pack("<BBHIIII", 1, 0, 0x8004, len(sid), 0, 0, 0)
|
||||
os.setxattr(path, "user.wine.sd", b"\x01\x00" + header + sid, follow_symlinks=False)
|
||||
|
||||
def heal(self):
|
||||
return MODULE["heal_shared_data"](str(self.compat))
|
||||
|
||||
def mode(self, path):
|
||||
return path.stat().st_mode & 0o777
|
||||
|
||||
def test_entries_a_stale_owner_locked_are_unlocked_and_every_stale_owner_is_cleared(self):
|
||||
agent = self.root / "Vendor/Agent"
|
||||
(agent / "Logs").mkdir(parents=True)
|
||||
(agent / "Agent.dat").write_text("1")
|
||||
(agent / "Tools.exe").write_text("x")
|
||||
(agent / "Icons").mkdir()
|
||||
for path in (agent.parent, agent, agent / "Logs", agent / "Agent.dat", agent / "Tools.exe", agent / "Icons"):
|
||||
self.own(path, self.STALE)
|
||||
(agent / "Tools.exe").chmod(0o550)
|
||||
(agent / "Icons").chmod(0o500)
|
||||
(agent / "Agent.dat").chmod(0)
|
||||
(agent / "Logs").chmod(0)
|
||||
agent.chmod(0)
|
||||
self.assertEqual((["Vendor/Agent", "Vendor/Agent/Agent.dat", "Vendor/Agent/Logs"],
|
||||
["Vendor", "Vendor/Agent", "Vendor/Agent/Agent.dat", "Vendor/Agent/Icons",
|
||||
"Vendor/Agent/Logs", "Vendor/Agent/Tools.exe"]), self.heal())
|
||||
self.assertEqual(0o500, self.mode(agent / "Icons"))
|
||||
self.assertEqual(0o700, self.mode(agent))
|
||||
self.assertEqual(0o700, self.mode(agent / "Logs"))
|
||||
self.assertEqual(0o600, self.mode(agent / "Agent.dat"))
|
||||
self.assertEqual(0o550, self.mode(agent / "Tools.exe"))
|
||||
self.assertEqual("1", (agent / "Agent.dat").read_text())
|
||||
(agent / "Logs/new").mkdir()
|
||||
for path in (agent.parent, agent, agent / "Logs", agent / "Agent.dat", agent / "Tools.exe", agent / "Icons"):
|
||||
self.assertNotIn("user.wine.sd", os.listxattr(path))
|
||||
self.assertEqual(([], []), self.heal())
|
||||
|
||||
def test_current_and_well_known_owners_untagged_entries_and_links_are_left_alone(self):
|
||||
mine = self.root / "Mine"
|
||||
mine.mkdir(mode=0o755)
|
||||
self.own(mine, self.CURRENT)
|
||||
(mine / "settings.ini").write_text("x")
|
||||
self.own(mine / "settings.ini", self.CURRENT)
|
||||
(mine / "settings.ini").chmod(0o444)
|
||||
denied = self.root / "Denied"
|
||||
denied.mkdir()
|
||||
self.own(denied, self.CURRENT)
|
||||
denied.chmod(0)
|
||||
admins = self.root / "Admins"
|
||||
admins.mkdir()
|
||||
self.own(admins, (32, 544))
|
||||
admins.chmod(0o500)
|
||||
plain = self.root / "Plain"
|
||||
plain.mkdir()
|
||||
plain.chmod(0)
|
||||
outside = Path(self.tmp.name) / "outside"
|
||||
outside.mkdir()
|
||||
self.own(outside, self.STALE)
|
||||
outside.chmod(0o500)
|
||||
(self.root / "Link").symlink_to(outside)
|
||||
self.assertEqual(([], []), self.heal())
|
||||
self.assertEqual(0o755, self.mode(mine))
|
||||
self.assertEqual(0o444, self.mode(mine / "settings.ini"))
|
||||
self.assertEqual(0, self.mode(denied))
|
||||
self.assertEqual(0o500, self.mode(admins))
|
||||
self.assertEqual(0, self.mode(plain))
|
||||
self.assertEqual(0o500, self.mode(outside))
|
||||
for path in (mine, mine / "settings.ini", admins, outside):
|
||||
self.assertIn("user.wine.sd", os.listxattr(path))
|
||||
|
||||
def test_a_locked_program_data_folder_is_healed_before_its_entries(self):
|
||||
(self.root / "Vendor").mkdir()
|
||||
self.own(self.root / "Vendor", self.STALE)
|
||||
(self.root / "Vendor").chmod(0)
|
||||
self.own(self.root, self.STALE)
|
||||
self.root.chmod(0)
|
||||
self.assertEqual(([".", "Vendor"], [".", "Vendor"]), self.heal())
|
||||
self.assertEqual(0o700, self.mode(self.root))
|
||||
self.assertEqual(0o700, self.mode(self.root / "Vendor"))
|
||||
|
||||
def test_a_heal_waits_while_another_launch_heals_the_same_prefix(self):
|
||||
(self.root / "Vendor").mkdir()
|
||||
self.own(self.root / "Vendor", self.STALE)
|
||||
(self.root / "Vendor").chmod(0)
|
||||
results = []
|
||||
lock = os.open(self.compat / "pfx", os.O_RDONLY | os.O_DIRECTORY)
|
||||
try:
|
||||
fcntl.flock(lock, fcntl.LOCK_EX)
|
||||
worker = threading.Thread(target=lambda: results.append(self.heal()))
|
||||
worker.start()
|
||||
worker.join(0.3)
|
||||
self.assertTrue(worker.is_alive())
|
||||
self.assertEqual(0, self.mode(self.root / "Vendor"))
|
||||
finally:
|
||||
os.close(lock)
|
||||
worker.join(10)
|
||||
self.assertEqual([(["Vendor"], ["Vendor"])], results)
|
||||
|
||||
def test_user_sid_follows_wine_for_a_missing_or_short_machine_id(self):
|
||||
sid = MODULE["wine_user_sid"]
|
||||
self.assertEqual(self.CURRENT, sid())
|
||||
self.globals["MACHINE_ID"] = str(Path(self.tmp.name) / "missing")
|
||||
self.assertEqual((21, 0, 0, 0, 1000), sid())
|
||||
short = Path(self.tmp.name) / "short"
|
||||
short.write_text("1234\n")
|
||||
self.globals["MACHINE_ID"] = str(short)
|
||||
self.assertEqual((21, 0, 0, 0, 1000), sid())
|
||||
binary = Path(self.tmp.name) / "binary"
|
||||
binary.write_bytes(b"\xff\xfe" + b"0" * 15)
|
||||
self.globals["MACHINE_ID"] = str(binary)
|
||||
self.assertEqual((21, 0, 0, os.getuid(), 1000), sid())
|
||||
signed = Path(self.tmp.name) / "signed"
|
||||
signed.write_text("+234abcd5678ef01\n")
|
||||
self.globals["MACHINE_ID"] = str(signed)
|
||||
self.assertEqual((21, 0x234abcd, 0x5678ef01, os.getuid(), 1000), sid())
|
||||
signed.write_text("-000000000000001\n")
|
||||
self.assertEqual((21, 0xffffffff, 0xffffffff, os.getuid(), 1000), sid())
|
||||
|
||||
def test_prefix_without_shared_data_is_skipped(self):
|
||||
self.assertEqual(([], []), MODULE["heal_shared_data"](str(Path(self.tmp.name) / "missing")))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
|
||||
Reference in new issue
Block a user