Skip to content

Reject symlink traversal in backup and restore - #37

Merged
loverustfs merged 7 commits into
mainfrom
cursor/backup-symlink-traversal-a4ed
Nov 20, 2026
Merged

loverustfs merged 7 commits into
mainfrom
cursor/backup-symlink-traversal-a4ed

Conversation

@loverustfs

@loverustfs loverustfs commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Backup and restore anchor at the operator-supplied data directory, the artifact directory, or the staging directory this process created. Each component below that anchor is one relative name. That name is rejected before any open when it is empty, ., .., absolute, or contains /, \, :, or NUL. The open does not follow a symlink.

On Unix the open is openat with O_NOFOLLOW (cap-primitives 4.0.3). On Windows it is CreateFile relative to the directory handle with FILE_FLAG_OPEN_REPARSE_POINT. A Windows reparse point that is_symlink does not report is rejected from FILE_ATTRIBUTE_REPARSE_POINT before its bytes are read. Linux directory descriptors that come back as O_PATH are reopened through /proc/self/fd/<n> for the same inode. That reopen does not walk the original path.

CURRENT is still checked as one relative file name before any child open. Publish and restore read the staged registry, Raft log, LSM CURRENT, SST names, and each cell_*/CELL_MANAGER_STATE through the directory handle. The shard-reference check receives those cell-state bytes. It does not call fs::read_dir or File::open on the staging path.

The live log path uses the same anchor. copy_durable_log still holds the log lock and syncs the active segment before reading. It does not call fs::read on segment.path.

This branch includes current main (36f50655: #40 control-plane observations and placement gating, plus the earlier readiness reporting and unauthenticated opt-in). Those docs and CELL_CONTROL_PLANE stay. committed_or_purged_index is unchanged.

Manifest CRC32C checks, safe_relative, rejection of a non-empty restore target, the outside-data-dir backup destination check, and the publish lock stay. There is no new command WAL and no transaction or on-disk format change.

Dependency decision

docs/DEVELOPMENT_PLAN.md §1.3 forbids unsafe C/C++ bindings (rocksdb / leveldb FFI). It does not forbid a pure-Rust syscall crate. deny.toml allows Apache-2.0, MIT, BSD-3-Clause, Unicode-3.0, and Zlib. Its graph targets are Linux only. multiple-versions is a warning.

Direct dependencies added on metadb-raft:

  • cap-primitives 4.0.3 — Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT
  • cap-fs-ext 4.0.3 with default features off, so cap-std is not pulled — same license expression

cap-fs-ext is only the public follow(FollowSymlinks::No) hook. Transitive crates on the Linux graph (ambient-authority, fs-set-times, io-extras, io-lifetimes 3.0.1, ipnet 2.12.2, maybe-owned, rustix-linux-procfs) use that same OR expression, or MIT OR Apache-2.0 for ipnet and maybe-owned. rustix stays 1.1.6, already in the lock. ipnet is compiled because cap-primitives depends on it unconditionally. It is not used by backup.

Windows-only winx 0.36.4 is Apache-2.0 WITH LLVM-exception and nothing else. That identifier is outside the deny.toml allow list. This change does not edit deny.toml, and it does not drop Windows from the graph to hide winx. No other published crate found here gives both Unix openat/O_NOFOLLOW and Windows handle-relative reparse opens under the allow list without new unsafe or without winx. Whether to keep cap-primitives is still an explicit policy decision. Backup behavior is unchanged while that decision is open.

Platform behavior

  • Linux, compiled here: O_NOFOLLOW on the final component, directory listing from the directory descriptor, pre-open name checks unchanged.
  • macOS would use the same Unix openat / O_NOFOLLOW path. It was not built or run here.
  • Windows would use handle-relative opens that do not follow a reparse point, then reject a reparse point before reading bytes. cap-primitives 4.0.3 directory listing turns the handle back into a path and calls std::fs::read_dir (read_dir without following symlinks is not implemented in that crate). Child bytes are still read through the handle. Windows was not built or run here.

What is enforced

  • open_child rejects a name that is not one relative component before it opens that name.
  • read_lsm_index and ManifestLog::read_checkpoint_readonly reject a bad CURRENT before opening the named manifest.
  • Staged registry bytes, the copied Raft log, the LSM image, and CELL_MANAGER_STATE are read from the staging directory handle before publish.
  • check_backup_tree parses those bytes. It does not open cell_*/CELL_MANAGER_STATE by path.
  • The live data-directory registry check under the publish lock uses that same anchored read.

What is not claimed

This is not a race-free copy.

  • A concurrent local writer can still replace a regular file with different bytes after it is listed and before those bytes are read.
  • A hard link that already lives inside the anchored directory is copied as that file's bytes.
  • Ancestors of the operator-supplied path are still resolved. The anchor itself must be a plain directory.
  • disk_floor and ordinary snapshot publication still open raft paths directly. Those paths are outside this copy walk.
  • Scratch cleanup empties the directory it opened, then removes it by name. A different empty directory swapped onto that name after the identity check can still be removed. There is no rmdir-by-inode.
  • On Windows, directory listing is path-based in cap-primitives 4.0.3. A name from that listing is not read unless the handle-relative open accepts it.
  • macOS and Windows were not compiled or run here.
  • winx 0.36.4 remains outside the license allow list. That policy choice is not made here.

Tests

This round compiled only. It did not run the suite.

  • cargo check --offline --tests -p metadb-raft -p metadb-storage -p metadb-server -p metadb-cluster finished with no new crate warnings.
  • Regression source, not executed this round: current_escape_is_rejected_before_the_external_file_is_opened and cell_state_symlink_is_rejected_before_the_external_file_is_opened. The second points a staging cell_ directory and CELL_MANAGER_STATE at a fifo and fails if a writer blocked on that fifo is released. It also rejects symlink metadata through reject_reparse.

Not run: the regression binaries, metadb-server integration tests, a macOS build, a Windows build, and CI. .github/workflows/ci.yml was not edited.

Leftovers

  • Still one data directory. Not a multi-Cell or Jumbo backup.
  • No independent Raft WAL.
  • Each copied file is still read into memory.
  • A symlink left at .backup-ckpt-<shard> is still not deleted.
Open in Web Open in Cursor 

Open copied directories with O_NOFOLLOW and walk the directory fd, so a
symlink, a non-UTF-8 name, or a special file fails the command instead of
importing or deleting an external target. Manifest CRC checks, safe
relative paths, and the existing publish and scratch behavior stay in
place. This does not freeze a regular file against a concurrent writer.

Co-authored-by: RustFS <hello@rustfs.com>
Copilot AI balanced review requested due to automatic review settings November 19, 2026 19:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

cursoragent and others added 6 commits November 19, 2026 19:48
Live raft copy reads through the data-directory fd while the log lock is
held, so a final or intermediate symlink is not followed. Backup create,
restore, and manifest publication walk from that anchor on Linux.
Other platforms compile and backup copy returns an unsupported error
instead of using Linux open flags. A symlink temporary manifest is not
truncated, and scratch cleanup removes the directory inode it opened.

Co-authored-by: RustFS <hello@rustfs.com>
A manifest name is checked as one relative component before any child
open, including open_child. Restore and publish read the staged registry,
Raft log, and LSM through the directory fd instead of reopening paths.
Linux open-flag constants are cfg-gated. macOS and Windows still have no
anchored backup copy; that remains an explicit unsupported path.

Co-authored-by: RustFS <hello@rustfs.com>
Bring in readiness reporting and explicit unauthenticated opt-in while
keeping the anchored backup copy.
Co-authored-by: RustFS <hello@rustfs.com>
Use cap-primitives 4.0.3 and cap-fs-ext 4.0.3 so Unix openat
O_NOFOLLOW and Windows handle-relative reparse opens share one
path. Names are still rejected before any open, and restore
checks stay on the directory handle.

Co-authored-by: RustFS <hello@rustfs.com>
The publish and restore checks were still walking the staging path
and opening CELL_MANAGER_STATE with File::open. They now receive
bytes already read with nofollow, so a swapped symlink is not opened.

Co-authored-by: RustFS <hello@rustfs.com>
Keep the control-plane observation work from #40 and the anchored
backup checks on this branch.
Co-authored-by: RustFS <hello@rustfs.com>
@loverustfs
loverustfs merged commit 16765da into main Nov 20, 2026
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.

3 participants