{"id":"GHSA-x9x8-xcv7-5x5g","summary":"termux-sync: Restore extracts archive members outside the backed-up directory because the extraction jail is the parent of each backup root","details":"## Summary\n\n`termux-sync restore` extracts each archive into the parent of the directory that archive is supposed to hold. The containment check then compares members against that wider directory, so an archive declared as `usr_etc` or `usr_lib` can write anywhere under `$PREFIX`, including `$PREFIX/bin`.\n\nThis is a residual gap in the fix for GHSA-259w-wmqg-fp76 / CVE-2026-86046. The guards that advisory added all work. What survives is the choice of destination they are checked against.\n\n## Affected versions\n\nTested on:\n\n- Package version: `termux-sync` 1.3.6 installed from PyPI (`termux_sync-1.3.6-py3-none-any.whl`)\n- Repository commit: `054e6d88acf16c7d9b025846bcc2652b2975c68a` on main\n- Python: 3.12.16, where `tarfile.data_filter` is available\n- Dependencies: `rich==15.0.0`, `markdown-it-py==4.3.0`, `mdurl==0.1.2`, `Pygments==2.21.0`\n- OS: Debian in a container, non-root, no network during the restore\n\nOnly 1.3.6 was tested. The primitive looks present in earlier releases, but unchecked. Releases before `9c8637b` are already covered by CVE-2026-86046.\n\nMain and the release differ only inside `cmd_daemon`. Lines 1 to 700, which hold all the archive and extraction code, are identical, so main has the same behaviour. `termux-sync.py` is a byte-identical copy of `src/termux_sync/__init__.py`, so the line numbers below apply to both.\n\n## Impact\n\nAn attacker who controls the content of a backup that a victim restores can write files anywhere under `$PREFIX`, overwriting existing ones. Replacing an executable such as `$PREFIX/bin/sh` gives code execution as the Termux user the next time anything invokes it, and it leaves dpkg's record of `$PREFIX` disagreeing with what is on disk.\n\nAttacker requirements:\n\n- Write access to the backup the victim restores. Nothing on the victim's device. This is the same position GHSA-259w-wmqg-fp76 described: \"a victim who restores a malicious backup (local, shared, or from a compromised GitHub/Google Drive storage backend)\". A backup shared between people moving to a new device is the use the README describes, and `SECURITY.md:41` already contemplates a GitHub storage repo being public.\n- The victim runs `termux-sync restore` and answers `y` at the prompt (`__init__.py:1599`).\n- No authentication, no root, and no non-default configuration. `$PREFIX` is writable by the Termux user by design, which is how `pkg install` works, so no privilege escalation is involved.\n\nThe restore preview (`__init__.py:1587-1596`) shows the backup name, date, package count and archive count. It does not list member paths, so a victim who inspects it cannot see `bin/sh` in a config archive.\n\n## Root cause\n\n`cmd_restore` builds the destination for each manifest key by taking the parent of the backup root (`src/termux_sync/__init__.py:1611-1614`):\n\n```python\n        allowed_roots = {}\n        for item_key, _label, item_root in BACKUP_ITEMS:\n            if item_root is not None:\n                allowed_roots[item_key] = Path(item_root).resolve().parent\n```\n\nWith `BACKUP_ITEMS` at `:69-74` and `TERMUX_HOME` = `$PREFIX`, `TERMUX_FILES` = `$HOME` (`:66-67`):\n\n| manifest key | backup root | resulting jail | what that jail holds |\n|---|---|---|---|\n| `home` | `$HOME` | `…/files` | all of `$PREFIX` |\n| `usr_etc` | `$PREFIX/etc` | `$PREFIX` | `bin/`, `lib/`, `libexec/`, `etc/` |\n| `usr_lib` | `$PREFIX/lib` | `$PREFIX` | same |\n\nThe extraction then passes that jail to `extract_archive` at `:1631`, and `_safe_extract_member` (`:555-582`) checks members against it:\n\n```python\n    target = (dest_path / member_name).resolve()\n    try:\n        target.relative_to(dest_root)\n```\n\nThat check is written correctly. Both sides are resolved, `..` and absolute members are rejected, link targets are checked, device and FIFO entries are refused, and `extract` runs with `filter=\"data\"` at `:601` and `:609`. A member named `bin/sh` in a `usr_etc` archive simply is inside `$PREFIX`, so it passes and gets written.\n\nThe comment three lines above `:1614` describes the invariant the code is meant to hold:\n\n```python\n        # Build the set of destinations this app is actually willing to restore into.\n        # Manifest-supplied \"source\" values are attacker-controlled backup content\n        # and must never be trusted as an extraction root — only used to double\n        # check they agree with the known-safe root for that item key.\n```\n\nThe known-safe root for `usr_etc` is `$PREFIX/etc`. The code uses `$PREFIX`.\n\nA backup the project's own code produces can never contain such a member. `create_archive` always adds one tree under `arcname=source_path.name` (`:545`, `:547`), so a real `usr_etc` archive holds only `etc/...`. Nothing checks that a member's first component matches the arcname the writer would have used.\n\n`$PREFIX/bin` is not part of the documented restore surface. `FAQ.md:80` lists the four components as the package list, the home directory, `$PREFIX/etc` and `$PREFIX/lib`, and `README.md:49` and `:272-276` say the same.\n\n## Reproduction\n\nThe PoC is local and non-destructive. It builds a fake Termux layout under a temp directory, points `HOME`, `PREFIX` and `TMPDIR` at it, and writes nothing outside it. Both roots come from those environment variables at `:66-67`, which is how the code behaves on a device.\n\nThe malicious backup has two archives. `usr_etc.tar.gz`, whose declared surface is `$PREFIX/etc`, contains `etc/motd` (legitimate), `bin/MARKER_from_usr_etc`, `bin/sh` at mode 0755 containing `echo ATTACKER_CODE_RUNS`, and two controls, `../escape_dotdot.txt` and `/abs_escape.txt`. `home.tar.gz`, declared surface `$HOME`, contains `home/.bashrc` (legitimate) and `usr/bin/MARKER_from_home`. Manifest checksums are correct so no checksum prompt fires, and the package list is empty so no `pkg` call happens.\n\nInstall:\n\n```bash\npython -m venv /tmp/venv && /tmp/venv/bin/pip install termux-sync==1.3.6\n```\n\nRun the restore through the real entry point:\n\n```bash\npython -I poc.py /path/to/termux-sync-wrapper\n```\n\nActual result:\n\n```text\n1.3.6 /work/venv/lib/python3.12/site-packages/termux_sync/__init__.py\n\n  Step 2/4  Verifying checksums…\n  ✓ usr_etc.tar.gz  832a54b0130e…\n  ✓ home.tar.gz  c2942b552bc8…\n  ⚠ This will overwrite existing files.\n  Proceed with restore? [y/n]:\n  Step 3/4  Extracting archives…\n  usr_etc.tar.gz ✓\n  home.tar.gz ✓\n  ✓ Restore completed successfully!\n\n=== RESULT ===\n  ESCAPE  $PREFIX/bin/MARKER_from_usr_etc      exists=True\n  ESCAPE  $PREFIX/bin/MARKER_from_home         exists=True\n  expected $PREFIX/etc/motd                    exists=True\n  expected $HOME/.bashrc                       exists=True\n  control files/escape_dotdot.txt              exists=False\n  control run/escape_dotdot.txt                exists=False\n  control /abs_escape.txt                      exists=False\n\n$PREFIX/bin/sh contents after restore:\n  #!/bin/sh\n  echo ATTACKER_CODE_RUNS\n\nlog tail:\n  [WARNING] Skipped unsafe archive member during restore: Path traversal attempt in archive: '../escape_dotdot.txt'\n  [WARNING] Skipped unsafe archive member during restore: Unsafe absolute path in archive: '/abs_escape.txt'\n  [INFO] Extracted usr_etc.tar.gz → /work/poc/run/files/usr\n  [INFO] Extracted home.tar.gz → /work/poc/run/files\n  [INFO] Restore completed from: termux_backup_evil\n```\n\nThe two control members were rejected and logged, which is the project's existing guards working. The two escapes are members that never leave the wider jail, so nothing rejects them. The log lines also show the separate destinations, `…/files/usr` for the config archive and `…/files` for the home archive, so each escape is independently attributable.\n\nThe mode survives `filter=\"data\"` and the replaced file runs:\n\n```text\n-rw-r--r-- 1 1000 1000  7 MARKER_from_home\n-rw-r--r-- 1 1000 1000  7 MARKER_from_usr_etc\n-rwxr-xr-x 1 1000 1000 34 sh\n---\nATTACKER_CODE_RUNS\n```\n\nExpected safe behaviour: a `usr_etc` archive can write only under `$PREFIX/etc`, and `$PREFIX/bin/sh` is untouched.\n\n## The narrow claim\n\nThe `home` variant is the weaker one. A `home` archive legitimately overwrites `$HOME/.bashrc`, which already gives code execution at the next login shell, so the escape adds less there.\n\nThe claim to keep is the `usr_etc` and `usr_lib` one. An attacker can ship a manifest that declares only `usr_etc`, with no `home` archive and nothing touching `$HOME`, and still take `$PREFIX/bin`. The victim consented to restoring configuration.\n\nOne related detail: `usr_lib` is in `BACKUP_ITEMS`, but `cmd_backup` only ever produces `home` and `usr_etc` archives (`:1394-1397`). A manifest containing a `usr_lib` key is therefore always forged, and it is accepted anyway.\n\n## Suggested fix\n\nJail each item to its own root instead of the parent, and drop the leading component when extracting. Alternatively keep extracting into the parent and require each member to start with the arcname that key would really produce, which is the smaller change and leaves the archive format alone:\n\n```python\nexpected = Path(item_root).name + \"/\"      # \"etc/\", \"lib/\", \"home/\"\nif member.name != expected.rstrip(\"/\") and not member.name.startswith(expected):\n    raise ValueError(...)\n```\n\n`_safe_extract_member` needs no change. A regression test fits next to `test_extract_rejects_hardlink_escaping_destination` in `tests/test_audit_fixes.py:200`: a `usr_etc` archive containing `bin/sh` must leave `$PREFIX/bin/sh` alone.\n\n## One thing that couldn't be tested\n\n`install_packages` (`:441-464`, called from `cmd_restore:1641`) passes manifest-supplied package names into `[\"pkg\", \"install\", \"-y\", pkg]`. A name starting with `-` would reach apt as an option rather than a package, and some apt options run commands. This could not be reproduced without Termux's `pkg` wrapper and a writable dpkg, so this is not claimed and not part of this report. A `--` separator before the package name would close it cheaply.\n\n## Scope of what was verified\n\nEverything above ran against the published 1.3.6 wheel, including the containment decision and the overwrite. The one simulated part is the filesystem layout: `$HOME` and `$PREFIX` point at container directories rather than a real Android install, and this wasn't run on a device.\n\nThe rest of the package appears clean. All ten `subprocess` call sites use argument lists with no `shell=True`. There is no `pickle`, `yaml.load`, `eval`, `exec` or `os.system` outside tests. Config is written 0o600 and re-chmodded (`:153-161`), `export-config` redacts the token (`:2319-2323`), and untrusted strings reaching the console go through `rich.markup.escape`. `tsctl` and `install.sh` extract a single member matched by name and clean up with a trap. The CI workflows take no untrusted PR content into a `run:` block.\n\n## Disclosure\n\nAI tooling was used while investigating and drafting this. Every measurement above comes from a run the reporter executed and checked, on the published package rather than a checkout, and the one simulated element is labelled where it appears.\n\n*Fix*: Fixed in 1.3.7. Restore now requires every archive member to start with the folder name the backup would have produced (`etc`, `lib`, `home`), and package names from the manifest are validated and passed to `pkg install` after `--`. Users should upgrade and only restore backups they created themselves.","aliases":["CVE-2026-141553"],"modified":"2026-12-03T02:45:05.621853163Z","published":"2026-12-03T02:36:05Z","database_specific":{"github_reviewed_at":"2026-12-03T02:36:05Z","nvd_published_at":"2026-12-02T20:20:40Z","cwe_ids":["CWE-22"],"severity":"MODERATE","github_reviewed":true},"references":[{"type":"WEB","url":"https://github.com/djunekz/termux-sync/security/advisories/GHSA-x9x8-xcv7-5x5g"},{"type":"ADVISORY","url":"https://nvd.nist.gov/vuln/detail/CVE-2026-141553"},{"type":"WEB","url":"https://github.com/djunekz/termux-sync/commit/3dd44a266cc9fba816c684164067616c7c0ef61d"},{"type":"WEB","url":"https://github.com/djunekz/termux-sync/commit/40a016331662c2c161f6045657b374fd00c05811"},{"type":"PACKAGE","url":"https://github.com/djunekz/termux-sync"},{"type":"WEB","url":"https://github.com/djunekz/termux-sync/releases/tag/v1.3.8"}],"affected":[{"package":{"name":"termux-sync","ecosystem":"PyPI","purl":"pkg:pypi/termux-sync"},"ranges":[{"type":"ECOSYSTEM","events":[{"introduced":"0"},{"fixed":"1.3.8"}]}],"versions":["1.1.0","1.1.1","1.1.2","1.2.0","1.2.1","1.2.2","1.2.3","1.2.4","1.2.6","1.2.8","1.3.0","1.3.5","1.3.6","1.3.7"],"database_specific":{"last_known_affected_version_range":"\u003c= 1.3.7","source":"https://github.com/github/advisory-database/blob/main/advisories/github-reviewed/2026/12/GHSA-x9x8-xcv7-5x5g/GHSA-x9x8-xcv7-5x5g.json"}}],"schema_version":"1.9.2","severity":[{"type":"CVSS_V3","score":"CVSS:3.1/AV:L/AC:L/PR:N/UI:R/S:U/C:N/I:H/A:N"}]}