Skip to content

Deck: run save readers in a bubblewrap sandbox - #4

Merged
Alexsander720 merged 2 commits into
Alexsander720:mainfrom
Guacam-Ole:feat/reader-sandbox
Nov 30, 2026
Merged

Alexsander720 merged 2 commits into
Alexsander720:mainfrom
Guacam-Ole:feat/reader-sandbox

Conversation

@Guacam-Ole

Copy link
Copy Markdown
Contributor

As this is a bit more complex I want to add some explanation beforehand. First of all this is no random AI Slop :)

The current problem is that every game is a Python script that can do ANYTHING on your machine. As this is a project that relies on many contributors (for games) I personally think is close to impossible to review every Game-PR if it does something "bad" (like doing stuff in your filesystem instead of just reading savefiles).

Therefore I added a sandbox that makes sure it only can access files it should. Tested in Big Picture Mode (Linux Mint) and on Steamdeck

Now to the details:

Problem and result

Every reader in ddsaves/ is a Python module imported into the backend process. A reader can therefore do anything the backend user can: read the whole home directory, open network connections, write files. The proof gate already runs contributions in an isolated subprocess, but the shipping plugin runs the exported readers in-process.

With this change each read() runs in a short-lived bubblewrap child:

  • read-only mounts only: /usr (system Python, openssl), plugin/py_modules, and the save locations for the reader's appid(s): Proton prefix, Steam userdata, game install folder, libraryfolders.vdf, /etc/localtime;
  • --unshare-all (no network, own pid/ipc namespaces), --clearenv, tmpfs /tmp, --die-with-parent, 10 s timeout, 512 MB RLIMIT_AS in the child;
  • the child prints one JSON card; validate_card() keeps only the card-contract keys, caps string/list sizes and nesting depth, and coerces fact fields to strings before anything reaches the frontend.

The appids a reader may see come from its APPID constants, parsed with ast (never imported), plus the appid currently displayed. deck_home() gains a CONTINUE_HOME override so the child resolves the same home as the host. Without bwrap or a system python3, or when the sandbox cannot start (user namespaces disabled), reads fall back to the previous in-process path; status() says why, the new backend method get_sandbox_status exposes it, and startup logs it once.

Scope

  • Plugin runtime (reader_sandbox.py, reader_sandbox_runner.py, deck_saves.py, ddsaves/_common.py, main.py)
  • Tests (tests/test_reader_sandbox.py)

Validation

  • Linux Mint 22.3, bubblewrap 0.9.0, Decky 3.3.1 (bundled Python 3.14 in the backend, system Python 3.12 in the child).
  • Startup log: reader sandbox {'sandboxed': True, 'bwrap': '/usr/bin/bwrap', 'python': '/usr/bin/python3', 'reason': None, 'fallbacks': 0}.
  • get_card for a real save through Decky: 30–45 ms round trip, card identical to the in-process reader (facts and observed_at; /etc/localtime is bound so save timestamps keep local time).
  • Isolation test from inside the child: home shows only the bound Steam path, a .ssh/canary outside it is invisible, socket.connect fails, writing next to the save fails, /root, /var, /opt do not exist.
  • python -m unittest discover -s tests: 39 tests OK (6 new; the isolation tests skip where bwrap is absent).
  • Steam Deck, SteamOS 3.9.5 (bubblewrap 0.12.0, system Python 3.14.6), Decky 3.3.1: startup log reports sandboxed: True, fallbacks: 0; the 6 sandbox tests pass on the Deck; the Graveyard Keeper 2 and Slay the Spire cards render as before (Steam Cloud saves under compatdata/<appid>). get_card round trip on the Deck is ~110 ms steady state (the sandboxed reader alone ~100 ms, of which nearly all is bwrap + interpreter start; the same reader in-process takes ~1 ms). On a Linux desktop the round trip is 30–45 ms.

Remaining limits

  • The sandbox protects against a malicious or buggy reader; main.py and the frontend still run with the backend's privileges.

Guacam-Ole and others added 2 commits November 26, 2026 16:07
…deck

deck_home() fell back to $HOME when /home/deck is absent, and main.py
hard-coded /home/deck/homebrew for the Game Theme Music cache. On a
desktop Linux install (Steam .deb, user is not "deck") the theme-music
lookup could never match, and a backend started as root (HOME=/root)
would find no library, prefix or save at all.

deck_home() now tries, in order: /home/deck, the sudo invoker, the single
/home/* account that owns a Steam installation, then $HOME. The theme
music path is derived from it. Behaviour on a Steam Deck is unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Readers are plain Python modules imported into the backend; in-process
they can do anything the backend user can (home, network, writes). This
runs each read() in a short-lived bwrap child instead:

- read-only: /usr, plugin/py_modules, and only the save locations for the
  reader's appid(s) (Proton prefix, Steam userdata, game install folder,
  libraryfolders.vdf, /etc/localtime);
- --unshare-all (no network, own pid/ipc), --clearenv, tmpfs /tmp,
  --die-with-parent, 10 s timeout, 512 MB RLIMIT_AS in the child;
- the child prints one JSON card; validate_card() keeps only the known
  keys, caps strings/lists/depth and coerces fact fields to strings.

Appids come from the reader's APPID constants, parsed with ast (never
imported), plus the appid being displayed. deck_home() honours a new
CONTINUE_HOME override so the child resolves the same home as the host.

Without bwrap or a system python3 (Windows, minimal images), or when the
sandbox cannot start (user namespaces disabled), reads fall back to the
previous in-process path; status() reports which, and the new backend
method get_sandbox_status exposes it. Startup logs the status once.

Measured on Decky 3.3.1 / Linux Mint: get_card round trip 30-45 ms with
the sandbox, identical cards to the in-process reader.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@Alexsander720

Copy link
Copy Markdown
Owner

Really appreciate you putting this together, and testing it on both Mint and an actual Steam Deck with timings. Having readers isolated in bubblewrap makes accepting community parsers way less scary, and it should help a lot if we submit to the Decky Store later.

I went through all 25 existing readers in ddsaves/ against the sandbox and ran into a few things that would break some current games if we merge as-is:

  1. validate_card() strips everything from each fact except id, icon, label, and value. Four shipped readers (god_of_war_ragnarok, sally_face, silent_hill_2, until_dawn) attach people or groups arrays to facts for character rosters and item/favor strips. For panel rows like {"id": "sf_panel", "groups": [...]}, stripping groups leaves an empty broken cell. We need validate_card() to keep and sanitize people, groups, and optional cls.
  2. Three readers look in native Linux save paths outside Proton/userdata: sally_face (~/.config/unity3d/Steve Gabry/Sally Face), undertale (~/.config/UNDERTALE, ~/.local/share/UNDERTALE), and slay_the_spire (~/.config/SlayTheSpire). Those won't be visible inside bwrap right now. Maybe we can let readers declare a NATIVE_DIRS tuple of relative paths under deck_home(), parsed via AST just like APPID, and bind them read-only with --ro-bind-try?
  3. _CURRENT = {"appid": None} in deck_saves.py is shared across executor threads, so scrolling fast between games could mix up appids. Switching it to threading.local() or passing appid directly would fix that.
  4. A few small hardening notes:
    • sandbox_exec() inherits Decky's PyInstaller env (LD_LIBRARY_PATH, _PYI*) when starting bwrap itself. Clearing those before subprocess.run (like standalone_media.py does) avoids library conflicts on other distros.
    • Using --ro-bind-try for game dirs too stops a transient missing path (like an unmounted microSD) from triggering disable() and turning off the sandbox for the whole session.
    • In test_reader_sandbox.py, HAVE_BWRAP checks if the binary exists, not whether unprivileged user namespaces are allowed, which can fail instead of skip in restricted CI containers.

If you want, I can push these fixes onto your branch so you can review them, or feel free to tweak it yourself. Thanks again for tackling this!

@Alexsander720
Alexsander720 merged commit a1bf58c into Alexsander720:main Nov 30, 2026
@Alexsander720

Copy link
Copy Markdown
Owner

Merged and shipped in v0.1.6, and the bubblewrap sandbox tests now run live in GitHub Actions too (v0.1.7). Thanks again for building this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants