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:
MaxsTechReview authored and GitHub committed 2026-11-25 05:48:37 -05:00
1 parent a6fe10d8ff
commit d38fd373f0
2 files changed
+290

No files matched your search

@@ -1,4 +1,5 @@
#!/usr/bin/python3
import fcntl
import hashlib
import json
import os
@@ -6,6 +7,8 @@ import platform
import re
import runpy
import signal
import stat
import struct
import subprocess
import sys
import tempfile
@@ -1046,6 +1049,9 @@ WINE_DLL_DIRS = ("/lib/wine/i386-windows", "/lib/wine/x86_64-windows", "/lib/win
FOREIGN_SUFFIX = ".foreign-arch"
WINE_SD = "user.wine.sd"
SHARED_DATA = "pfx/drive_c/ProgramData"
MACHINE_ID = "/etc/machine-id"
PE = runpy.run_path(str(Path(__file__).resolve().parent / "droiddeck-wine-pe"))
pe_machine = PE["pe_machine"]
@@ -1144,6 +1150,124 @@ def repair_foreign_system_dlls(prefix, proton, env=None):
return repaired
def wine_user_sid():
try:
with open(MACHINE_ID, "rb") as handle:
text = handle.read(16)
except OSError:
return (21, 0, 0, 0, 1000)
if len(text) != 16:
return (21, 0, 0, 0, 1000)
sign, digits = re.match(rb"\s*([+-]?)(?:0[xX])?([0-9a-fA-F]*)", text).groups()
machine = int(digits, 16) if digits else 0
if sign == b"-":
machine = -machine % (1 << 64)
return (21, machine >> 32, machine & 0xffffffff, os.getuid(), 1000)
def wine_sd_owner(path):
try:
data = os.getxattr(path, WINE_SD, follow_symlinks=False)
except OSError:
return None
if len(data) < 22:
return None
sid = data[22:22 + struct.unpack_from("<I", data, 6)[0]]
if len(sid) < 8 or len(sid) < 8 + 4 * sid[1]:
return None
return sid[0], int.from_bytes(sid[2:8], "big"), struct.unpack_from("<%dI" % sid[1], sid, 8)
def is_stale_owner(owner, user):
if owner is None:
return False
revision, authority, subs = owner
return revision == 1 and authority == 5 and len(subs) == 5 and subs[0] == 21 and subs[4] == 1000 \
and subs != user
def heal_wine_entry(path, is_dir, user):
try:
mode = stat.S_IMODE(os.lstat(path).st_mode)
except OSError:
return None, False
readable = mode
if not mode & stat.S_IRUSR:
readable = mode | stat.S_IRUSR
try:
os.chmod(path, readable)
except OSError:
return None, False
final = mode
stale = is_stale_owner(wine_sd_owner(path), user)
cleared = False
if stale and (not mode & stat.S_IRUSR or is_dir and not mode & stat.S_IXUSR):
final = mode | (stat.S_IRWXU if is_dir else stat.S_IRUSR | stat.S_IWUSR)
try:
if stale:
os.chmod(path, final | stat.S_IWUSR)
try:
os.removexattr(path, WINE_SD, follow_symlinks=False)
cleared = True
except OSError:
pass
if not final & stat.S_IWUSR:
os.chmod(path, final)
elif final != readable:
os.chmod(path, final)
except OSError:
return None, cleared
return (mode, final), cleared
def heal_shared_data(prefix):
root = Path(prefix) / SHARED_DATA
if root.is_symlink() or not root.is_dir():
return [], []
try:
lock = os.open(root.parent.parent, os.O_RDONLY | os.O_DIRECTORY)
except OSError:
return [], []
try:
fcntl.flock(lock, fcntl.LOCK_EX)
return heal_shared_data_locked(root)
finally:
os.close(lock)
def heal_shared_data_locked(root):
user = wine_user_sid()
unlocked = []
cleared = []
pending = [(str(root), True)]
while pending:
path, is_dir = pending.pop()
modes, was_cleared = heal_wine_entry(path, is_dir, user)
name = os.path.relpath(path, root)
if was_cleared:
cleared.append(name)
if modes is None:
continue
mode, final = modes
if final != mode:
unlocked.append(name)
if not is_dir or final & (stat.S_IRUSR | stat.S_IXUSR) != stat.S_IRUSR | stat.S_IXUSR:
continue
try:
with os.scandir(path) as entries:
for entry in entries:
try:
if entry.is_dir(follow_symlinks=False):
pending.append((entry.path, True))
elif entry.is_file(follow_symlinks=False):
pending.append((entry.path, False))
except OSError:
continue
except OSError:
continue
return sorted(unlocked), sorted(cleared)
def is_wine_dll_link(path):
try:
return os.path.islink(path) and os.path.dirname(os.readlink(path)).endswith(WINE_DLL_DIRS)
@@ -1486,6 +1610,14 @@ def main():
except OSError as error:
restored = fixed = []
print("droiddeck-game-env: prefix system DLL check skipped (%s)" % error, file=sys.stderr)
try:
unlocked, cleared = heal_shared_data(prefix_path)
except OSError as error:
unlocked = cleared = []
print("droiddeck-game-env: ProgramData owner check skipped (%s)" % error, file=sys.stderr)
if cleared:
print("droiddeck-game-env: cleared %d stale Windows owners in ProgramData; unlocked: %s"
% (len(cleared), ", ".join(unlocked) or "none"), file=sys.stderr)
if restored:
print("droiddeck-game-env: restored system DLLs this Proton's own programs do not load: %s" % ", ".join(restored),
file=sys.stderr)
+158
View File
@@ -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()