Skip to content

Commit 2e4786f

Browse files
committed
fix: add path sanitization and EOF validation in CopyFromInstance
- Use securejoin to prevent path traversal attacks - Return error when stream ends without completion marker
1 parent 5274cb2 commit 2e4786f

File tree

4 files changed

+86
-4
lines changed

4 files changed

+86
-4
lines changed

cmd/api/api/cp.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ func (s *ApiService) handleCopyFrom(ctx context.Context, ws *websocket.Conn, ins
290290
return fmt.Errorf("start copy stream: %w", err)
291291
}
292292

293+
var receivedFinal bool
294+
293295
// Stream responses to WebSocket client
294296
for {
295297
resp, err := stream.Recv()
@@ -334,6 +336,7 @@ func (s *ApiService) handleCopyFrom(ctx context.Context, ws *websocket.Conn, ins
334336
return fmt.Errorf("write end: %w", err)
335337
}
336338
if r.End.Final {
339+
receivedFinal = true
337340
return nil
338341
}
339342

@@ -349,6 +352,9 @@ func (s *ApiService) handleCopyFrom(ctx context.Context, ws *websocket.Conn, ins
349352
}
350353
}
351354

355+
if !receivedFinal {
356+
return fmt.Errorf("copy stream ended without completion marker")
357+
}
352358
return nil
353359
}
354360

docker-cp.txt

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
docker container cp
2+
Description Copy files/folders between a container and the local filesystem
3+
Usage docker container cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|- docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
4+
Aliases
5+
docker cp
6+
Description
7+
The docker cp utility copies the contents of SRC_PATH to the DEST_PATH. You can copy from the container's file system to the local machine or the reverse, from the local filesystem to the container. If - is specified for either the SRC_PATH or DEST_PATH, you can also stream a tar archive from STDIN or to STDOUT. The CONTAINER can be a running or stopped container. The SRC_PATH or DEST_PATH can be a file or directory.
8+
9+
The docker cp command assumes container paths are relative to the container's / (root) directory. This means supplying the initial forward slash is optional; The command sees compassionate_darwin:/tmp/foo/myfile.txt and compassionate_darwin:tmp/foo/myfile.txt as identical. Local machine paths can be an absolute or relative value. The command interprets a local machine's relative paths as relative to the current working directory where docker cp is run.
10+
11+
The cp command behaves like the Unix cp -a command in that directories are copied recursively with permissions preserved if possible. Ownership is set to the user and primary group at the destination. For example, files copied to a container are created with UID:GID of the root user. Files copied to the local machine are created with the UID:GID of the user which invoked the docker cp command. However, if you specify the -a option, docker cp sets the ownership to the user and primary group at the source. If you specify the -L option, docker cp follows any symbolic link in the SRC_PATH. docker cp doesn't create parent directories for DEST_PATH if they don't exist.
12+
13+
Assuming a path separator of /, a first argument of SRC_PATH and second argument of DEST_PATH, the behavior is as follows:
14+
15+
SRC_PATH specifies a file
16+
DEST_PATH does not exist
17+
the file is saved to a file created at DEST_PATH
18+
DEST_PATH does not exist and ends with /
19+
Error condition: the destination directory must exist.
20+
DEST_PATH exists and is a file
21+
the destination is overwritten with the source file's contents
22+
DEST_PATH exists and is a directory
23+
the file is copied into this directory using the basename from SRC_PATH
24+
SRC_PATH specifies a directory
25+
DEST_PATH does not exist
26+
DEST_PATH is created as a directory and the contents of the source directory are copied into this directory
27+
DEST_PATH exists and is a file
28+
Error condition: cannot copy a directory to a file
29+
DEST_PATH exists and is a directory
30+
SRC_PATH does not end with /. (that is: slash followed by dot)
31+
the source directory is copied into this directory
32+
SRC_PATH does end with /. (that is: slash followed by dot)
33+
the content of the source directory is copied into this directory
34+
The command requires SRC_PATH and DEST_PATH to exist according to the above rules. If SRC_PATH is local and is a symbolic link, the symbolic link, not the target, is copied by default. To copy the link target and not the link, specify the -L option.
35+
36+
A colon (:) is used as a delimiter between CONTAINER and its path. You can also use : when specifying paths to a SRC_PATH or DEST_PATH on a local machine, for example file:name.txt. If you use a : in a local machine path, you must be explicit with a relative or absolute path, for example:
37+
38+
`/path/to/file:name.txt` or `./file:name.txt`
39+
Options
40+
Option Default Description
41+
-a, --archive Archive mode (copy all uid/gid information)
42+
-L, --follow-link Always follow symbol link in SRC_PATH
43+
-q, --quiet Suppress progress output during copy. Progress output is automatically suppressed if no terminal is attached
44+
Examples
45+
Copy a local file into container
46+
47+
48+
$ docker cp ./some_file CONTAINER:/work
49+
Copy files from container to local path
50+
51+
52+
$ docker cp CONTAINER:/var/logs/ /tmp/app_logs
53+
Copy a file from container to stdout. Note cp command produces a tar stream
54+
55+
56+
$ docker cp CONTAINER:/var/logs/app.log - | tar x -O | grep "ERROR"
57+
Corner cases
58+
It isn't possible to copy certain system files such as resources under /proc, /sys, /dev, tmpfs, and mounts created by the user in the container. However, you can still copy such files by manually running tar in docker exec. Both of the following examples do the same thing in different ways (consider SRC_PATH and DEST_PATH are directories):
59+
60+
61+
$ docker exec CONTAINER tar Ccf $(dirname SRC_PATH) - $(basename SRC_PATH) | tar Cxf DEST_PATH -
62+
63+
$ tar Ccf $(dirname SRC_PATH) - $(basename SRC_PATH) | docker exec -i CONTAINER tar Cxf DEST_PATH -
64+
Using - as the SRC_PATH streams the contents of STDIN as a tar archive. The command extracts the content of the tar to the DEST_PATH in container's filesystem. In this case, DEST_PATH must specify a directory. Using - as the DEST_PATH streams the contents of the resource as a tar archive to STDOUT.

guest_agent

14.7 MB
Binary file not shown.

lib/guest/client.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"sync/atomic"
1616
"time"
1717

18+
securejoin "github.com/cyphar/filepath-securejoin"
1819
"google.golang.org/grpc"
1920
"google.golang.org/grpc/credentials/insecure"
2021
)
@@ -466,6 +467,7 @@ func CopyFromInstance(ctx context.Context, vsockSocketPath string, opts CopyFrom
466467

467468
var currentFile *os.File
468469
var currentHeader *CopyFromGuestHeader
470+
var receivedFinal bool
469471

470472
// Ensure file is closed on error paths
471473
defer func() {
@@ -492,7 +494,11 @@ func CopyFromInstance(ctx context.Context, vsockSocketPath string, opts CopyFrom
492494
}
493495

494496
currentHeader = r.Header
495-
targetPath := filepath.Join(opts.DstPath, r.Header.Path)
497+
// Use securejoin to prevent path traversal attacks
498+
targetPath, err := securejoin.SecureJoin(opts.DstPath, r.Header.Path)
499+
if err != nil {
500+
return fmt.Errorf("invalid path %s: %w", r.Header.Path, err)
501+
}
496502

497503
if r.Header.IsDir {
498504
if err := os.MkdirAll(targetPath, fs.FileMode(r.Header.Mode)); err != nil {
@@ -529,14 +535,17 @@ func CopyFromInstance(ctx context.Context, vsockSocketPath string, opts CopyFrom
529535
currentFile.Close()
530536
// Set modification time
531537
if currentHeader != nil && currentHeader.Mtime > 0 {
532-
targetPath := filepath.Join(opts.DstPath, currentHeader.Path)
533-
mtime := time.Unix(currentHeader.Mtime, 0)
534-
os.Chtimes(targetPath, mtime, mtime)
538+
targetPath, err := securejoin.SecureJoin(opts.DstPath, currentHeader.Path)
539+
if err == nil {
540+
mtime := time.Unix(currentHeader.Mtime, 0)
541+
os.Chtimes(targetPath, mtime, mtime)
542+
}
535543
}
536544
currentFile = nil
537545
currentHeader = nil
538546
}
539547
if r.End.Final {
548+
receivedFinal = true
540549
return nil
541550
}
542551

@@ -545,6 +554,9 @@ func CopyFromInstance(ctx context.Context, vsockSocketPath string, opts CopyFrom
545554
}
546555
}
547556

557+
if !receivedFinal {
558+
return fmt.Errorf("copy stream ended without completion marker")
559+
}
548560
return nil
549561
}
550562

0 commit comments

Comments
 (0)