Skip to main content
Menu
Advisories

croc symlink metadata can overwrite arbitrary receiver files during file transfer

GHSA-M6M7-376M-RR8GPublished Updated GitHub

Description

A malicious sender that knows a croc transfer code can make the receiver write attacker-controlled bytes outside the selected receive directory. The receiver accepts sender-controlled symlink metadata, creates the symlink, and later opens a same-name regular file with os.OpenFile without O_NOFOLLOW or a post-symlink check. If the malicious file list contains a symlink entry followed by a regular file entry with the same FolderRemote and Name, the second receive step follows the symlink and overwrites the symlink target as the receiver's local user.

A related lower-impact gap exists for empty folders: EmptyFoldersToTransfer[].FolderRemote is not validated with the same local-path checks applied to regular file names, so a sender can create directories outside the receive directory.

Impact

An attacker who convinces a victim to receive a croc transfer with a shared code can overwrite any file that the victim's OS user can write, even when the victim intended to receive files only into the current directory or --out directory. Practical targets include shell startup files, application configuration, project files, or other user-writable files. This can cause arbitrary data corruption and may lead to code execution when overwritten configuration or startup files are later used.

The attack does not require control of the relay and does not require breaking PAKE. It is carried inside the encrypted file metadata from the authenticated sender.

Reproduction

The following focused Go test demonstrates the issue against the current code. It creates a receiver directory and a separate outside-target.txt, then simulates the decrypted file metadata that a malicious sender can send:

  1. File entry 0 is a symlink named dup whose target is outside-target.txt outside the receiver directory.
  2. File entry 1 is a regular file also named dup with attacker-controlled content.
  3. The receiver creates the symlink for entry 0.
  4. When processing entry 1, recipientInitializeFile opens ./dup with os.OpenFile, follows the symlink, truncates the target, and writes the attacker payload.

Test command:

go test ./src/croc -run 'TestHostile(SymlinkThenSameNameFileOverwritesSymlinkTarget|EmptyFolderTraversalCreatesOutsideCwd|RegularFileTraversalRejected)' -count=1 -v

Observed result:

=== RUN   TestHostileSymlinkThenSameNameFileOverwritesSymlinkTarget
--- PASS: TestHostileSymlinkThenSameNameFileOverwritesSymlinkTarget (0.00s)
=== RUN   TestHostileEmptyFolderTraversalCreatesOutsideCwd
--- PASS: TestHostileEmptyFolderTraversalCreatesOutsideCwd (0.00s)
=== RUN   TestHostileRegularFileTraversalRejected
--- PASS: TestHostileRegularFileTraversalRejected (0.00s)
PASS

The test verifies that the regular-file path traversal check rejects ../escaped-file/x.txt, while the symlink overwrite and empty-folder traversal paths are still accepted.

Root Cause and Technical Details

In src/croc/croc.go, processMessageFileInfo validates regular file paths by cleaning FolderRemote, joining it with Name, and calling utils.ValidFileName:

for i, fi := range c.FilesToTransfer {
    c.FilesToTransfer[i].FolderRemote = filepath.Clean(fi.FolderRemote)
    if strings.Contains(c.FilesToTransfer[i].FolderRemote, ".ssh") {
        return true, fmt.Errorf("invalid path detected: '%s'", fi.FolderRemote)
    }
    errFileName := utils.ValidFileName(path.Join(c.FilesToTransfer[i].FolderRemote, fi.Name))
    if errFileName != nil {
        return true, errFileName
    }
}

However, the symlink target in FileInfo.Symlink is not constrained. Later, createEmptyFileAndFinish creates that symlink directly:

pathToFile := path.Join(fileInfo.FolderRemote, fileInfo.Name)
if fileInfo.Symlink != "" {
    if _, errExists := os.Lstat(pathToFile); errExists == nil {
        os.Remove(pathToFile)
    }
    err = os.Symlink(fileInfo.Symlink, pathToFile)
}

When a later same-name regular file is processed, recipientInitializeFile opens the path with os.OpenFile and follows the symlink:

pathToFile := path.Join(
    c.FilesToTransfer[c.FilesToTransferCurrentNum].FolderRemote,
    c.FilesToTransfer[c.FilesToTransferCurrentNum].Name,
)
c.CurrentFile, errOpen = os.OpenFile(pathToFile, os.O_WRONLY, 0o666)

The empty-folder path has a separate validation gap. EmptyFoldersToTransfer is copied from the sender and later used directly:

c.EmptyFoldersToTransfer = senderInfo.EmptyFoldersToTransfer
...
err = os.MkdirAll(c.EmptyFoldersToTransfer[i].FolderRemote, os.ModePerm)

Regular file traversal is rejected, but empty folder paths are not checked with utils.ValidFileName or filepath.IsLocal.

The vulnerable code exists in current main at commit 15424f504553353d1633e4dff528bdf9fcc0dd1d and in the latest release tag v10.4.4.

Remediation

Consider applying all of the following defenses:

  1. Reject absolute, parent-relative, or non-local FolderRemote values for both regular files and EmptyFoldersToTransfer before any filesystem operation.
  2. Reject duplicate destination paths in a single transfer after path normalization.
  3. Do not follow symlinks when opening receive destinations. Use platform-appropriate no-follow behavior, or Lstat immediately before opening and reject symlinks.
  4. Consider disabling symlink materialization by default, or only preserve symlinks after an explicit receiver opt-in.
  5. Validate symlink targets if symlink preservation remains supported. At minimum, reject absolute targets and targets that resolve outside the receive root.

Affected versions

References

GitHub · schollz/croc