# GHSA-X9X8-XCV7-5X5G: termux-sync: Restore extracts archive members outside the backed-up directory because the extraction jail is the parent of each backup root

- Database: GitHub
- Severity: CVSS 5.5 (Medium)
- Published: 2026-12-03
- Modified: 2026-12-03

## Tracked as

- [CVE-2026-141553](https://cve.now/CVE-2026-141553.md): termux-sync: Restore extracts archive members outside the backed-up directory because the extraction jail is the parent of each backup root

## Affected versions

- termux-sync (PyPI): fixed in 1.3.8

## Description

## Summary

`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`.

This 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.

## Affected versions

Tested on:

- Package version: `termux-sync` 1.3.6 installed from PyPI (`termux_sync-1.3.6-py3-none-any.whl`)
- Repository commit: `054e6d88acf16c7d9b025846bcc2652b2975c68a` on main
- Python: 3.12.16, where `tarfile.data_filter` is available
- Dependencies: `rich==15.0.0`, `markdown-it-py==4.3.0`, `mdurl==0.1.2`, `Pygments==2.21.0`
- OS: Debian in a container, non-root, no network during the restore

Only 1.3.6 was tested. The primitive looks present in earlier releases, but unchecked. Releases before `9c8637b` are already covered by CVE-2026-86046.

Main 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.

## Impact

An 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.

Attacker requirements:

- 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.
- The victim runs `termux-sync restore` and answers `y` at the prompt (`__init__.py:1599`).
- 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.

The 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.

## Root cause

`cmd_restore` builds the destination for each manifest key by taking the parent of the backup root (`src/termux_sync/__init__.py:1611-1614`):

```python
        allowed_roots = {}
        for item_key, _label, item_root in BACKUP_ITEMS:
            if item_root is not None:
                allowed_roots[item_key] = Path(item_root).resolve().parent
```

With `BACKUP_ITEMS` at `:69-74` and `TERMUX_HOME` = `$PREFIX`, `TERMUX_FILES` = `$HOME` (`:66-67`):

| manifest key | backup root | resulting jail | what that jail holds |
|---|---|---|---|
| `home` | `$HOME` | `…/files` | all of `$PREFIX` |
| `usr_etc` | `$PREFIX/etc` | `$PREFIX` | `bin/`, `lib/`, `libexec/`, `etc/` |
| `usr_lib` | `$PREFIX/lib` | `$PREFIX` | same |

The extraction then passes that jail to `extract_archive` at `:1631`, and `_safe_extract_member` (`:555-582`) checks members against it:

```python
    target = (dest_path / member_name).resolve()
    try:
        target.relative_to(dest_root)
```

That 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.

The comment three lines above `:1614` describes the invariant the code is meant to hold:

```python
        # Build the set of destinations this app is actually willing to restore into.
        # Manifest-supplied "source" values are attacker-controlled backup content
        # and must never be trusted as an extraction root — only used to double
        # check they agree with the known-safe root for that item key.
```

The known-safe root for `usr_etc` is `$PREFIX/etc`. The code uses `$PREFIX`.

A 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.

`$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.

## Reproduction

The 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.

The 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.

Install:

```bash
python -m venv /tmp/venv && /tmp/venv/bin/pip install termux-sync==1.3.6
```

Run the restore through the real entry point:

```bash
python -I poc.py /path/to/termux-sync-wrapper
```

Actual result:

```text
1.3.6 /work/venv/lib/python3.12/site-packages/termux_sync/__init__.py

  Step 2/4  Verifying checksums…
  ✓ usr_etc.tar.gz  832a54b0130e…
  ✓ home.tar.gz  c2942b552bc8…
  ⚠ This will overwrite existing files.
  Proceed with restore? [y/n]:
  Step 3/4  Extracting archives…
  usr_etc.tar.gz ✓
  home.tar.gz ✓
  ✓ Restore completed successfully!

=== RESULT ===
  ESCAPE  $PREFIX/bin/MARKER_from_usr_etc      exists=True
  ESCAPE  $PREFIX/bin/MARKER_from_home         exists=True
  expected $PREFIX/etc/motd                    exists=True
  expected $HOME/.bashrc                       exists=True
  control files/escape_dotdot.txt              exists=False
  control run/escape_dotdot.txt                exists=False
  control /abs_escape.txt                      exists=False

$PREFIX/bin/sh contents after restore:
  #!/bin/sh
  echo ATTACKER_CODE_RUNS

log tail:
  [WARNING] Skipped unsafe archive member during restore: Path traversal attempt in archive: '../escape_dotdot.txt'
  [WARNING] Skipped unsafe archive member during restore: Unsafe absolute path in archive: '/abs_escape.txt'
  [INFO] Extracted usr_etc.tar.gz → /work/poc/run/files/usr
  [INFO] Extracted home.tar.gz → /work/poc/run/files
  [INFO] Restore completed from: termux_backup_evil
```

The 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.

The mode survives `filter="data"` and the replaced file runs:

```text
-rw-r--r-- 1 1000 1000  7 MARKER_from_home
-rw-r--r-- 1 1000 1000  7 MARKER_from_usr_etc
-rwxr-xr-x 1 1000 1000 34 sh
---
ATTACKER_CODE_RUNS
```

Expected safe behaviour: a `usr_etc` archive can write only under `$PREFIX/etc`, and `$PREFIX/bin/sh` is untouched.

## The narrow claim

The `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.

The 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.

One 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.

## Suggested fix

Jail 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:

```python
expected = Path(item_root).name + "/"      # "etc/", "lib/", "home/"
if member.name != expected.rstrip("/") and not member.name.startswith(expected):
    raise ValueError(...)
```

`_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.

## One thing that couldn't be tested

`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.

## Scope of what was verified

Everything 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.

The 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.

## Disclosure

AI 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.

*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.

## References

- https://github.com/djunekz/termux-sync/security/advisories/GHSA-x9x8-xcv7-5x5g
- https://nvd.nist.gov/vuln/detail/CVE-2026-141553
- https://github.com/djunekz/termux-sync/commit/3dd44a266cc9fba816c684164067616c7c0ef61d
- https://github.com/djunekz/termux-sync/commit/40a016331662c2c161f6045657b374fd00c05811
- https://github.com/djunekz/termux-sync
- https://github.com/djunekz/termux-sync/releases/tag/v1.3.8

Page: https://cve.now/GHSA-X9X8-XCV7-5X5G

Wrong or missing data? Send feedback: POST https://cve.now/api/v1/feedback (see https://cve.now/agents#feedback).
