Cross-Bucket Path Traversal in SFTP/HDFS/NFS/Gluster/CIFS Backends
Root Cause
pkg/object/file.go:77-96 implements a boundary check in the path() method that validates resolved paths stay within the storage root using filepath.Rel:
func (d *filestore) path(key string) (string, error) {
p := filepath.Join(d.root, key)
boundary := d.root
rel, err := filepath.Rel(boundary, p)
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
return "", fmt.Errorf("object key %q escapes storage root %q", key, d.root)
}
return p, nil
}Five other filesystem-based backends perform the identical root+key concatenation but lack any boundary check:
SFTP — pkg/object/sftp.go:159:
func (f *sftpStore) path(key string) string {
return f.root + key
}HDFS — pkg/object/hdfs.go:59:
func (h *hdfsclient) path(key string) string {
return h.basePath + key
}NFS — pkg/object/nfs.go:90:
func (n *nfsStore) path(key string) string {
if key == "" { return "./" }
return key
}Gluster — pkg/object/gluster.go: No path() method at all; keys are passed directly to g.vol().Stat(key), g.vol().Open(key), etc.
CIFS/SMB — pkg/object/cifs.go: Keys are passed directly to share.Lstat(key), share.Open(key), share.Create(tmp).
The filestore.path() returns (string, error), enabling callers to check for escape. The vulnerable backends return only string — there is no mechanism to reject traversal.
Attack Chain
The primary vector is juicefs sync, which copies objects from a source storage to a destination:
- Attacker places a file with a malicious name (e.g.,
../../etc/cron.d/backdoor) in a source storage. - Victim runs
juicefs sync source:/ sftp://user@host:/data/backup/to sync to an SFTP-backed destination. - The sync engine lists objects from the source and calls
dst.Put(ctx, key, reader)for each one (pkg/sync/sync.go:752). sftpStore.Put()(line 215) callsf.path(key)which returnsf.root + key=/data/backup/../../etc/cron.d/backdoorwith no validation.sftpStore.Put()at line 226 callsc.sftpClient.MkdirAll(filepath.Dir(p)), creating parent directories for the traversed path.- The file is written to
/etc/cron.d/backdooron the SFTP server.
The same attack works against HDFS, NFS, Gluster, and CIFS destinations.
Reproduction
Requires: Go 1.21+.
Run exploit.sh — it clones JuiceFS from source, places a test file in the pkg/object package that starts a real in-process SSH+SFTP server (using crypto/ssh and github.com/pkg/sftp, both already in JuiceFS's dependency tree), then creates a real sftpStore via newSftp() connected to this server.
The test calls sftpStore.Put(ctx, "../../pwned.txt", data) through the real SFTP connection. The path() method at sftp.go:159 returns root + "../../pwned.txt" with no boundary check, and Put() writes the file via SFTP to the resolved path — outside the storage root.
Zero mock: real SSH server, real SFTP subsystem, real sftpStore created by the real newSftp() factory, real Put() writing through the SFTP protocol. The file pwned.txt is confirmed written to the parent of the storage root's grandparent.
Impact
An attacker who can influence object keys synced to a vulnerable backend (SFTP, HDFS, NFS, Gluster, CIFS) can achieve:
- Arbitrary file write:
Putwith../../key writes outside the storage root, overwriting system files (/etc/cron.d/,~/.ssh/authorized_keys, etc.) - Arbitrary file read:
Getwith../../key reads files outside the storage root - Arbitrary file delete:
Deletewith../../key removes files outside the storage root - Directory creation: SFTP
PutcallsMkdirAll(filepath.Dir(p))(line 226), creating intermediate directories for the traversed path
The filestore backend received a correct fix (the filepath.Rel boundary check at lines 85-93). The same fix was not applied to any of the five other filesystem-based backends, leaving them vulnerable to the same attack class.
Suggested Fix
Apply the same boundary check from filestore.path() to all filesystem-based backends. Change the return type to (string, error):
func (f *sftpStore) path(key string) (string, error) {
p := filepath.Clean(f.root + key)
rel, err := filepath.Rel(f.root, p)
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
return "", fmt.Errorf("object key %q escapes storage root %q", key, f.root)
}
return p, nil
}Source: juicedata/juicefs