Skip to content

Commit 49ca87d

Browse files
author
Kazuyoshi Kato
committed
Limit the response size of ExecSync
Signed-off-by: Kazuyoshi Kato <[email protected]>
1 parent da2db02 commit 49ca87d

2 files changed

Lines changed: 86 additions & 2 deletions

File tree

pkg/cri/server/container_execsync.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package server
1919
import (
2020
"bytes"
2121
"context"
22+
"errors"
2223
"fmt"
2324
"io"
2425
"syscall"
@@ -38,14 +39,48 @@ import (
3839
cioutil "github.com/containerd/containerd/pkg/ioutil"
3940
)
4041

42+
type cappedWriter struct {
43+
w io.WriteCloser
44+
remain int
45+
}
46+
47+
var errNoRemain = errors.New("no more space to write")
48+
49+
func (cw *cappedWriter) Write(p []byte) (int, error) {
50+
if cw.remain <= 0 {
51+
return 0, errNoRemain
52+
}
53+
54+
end := cw.remain
55+
if end > len(p) {
56+
end = len(p)
57+
}
58+
written, err := cw.w.Write(p[0:end])
59+
cw.remain -= written
60+
61+
if err != nil {
62+
return written, err
63+
}
64+
if written < len(p) {
65+
return written, errNoRemain
66+
}
67+
return written, nil
68+
}
69+
70+
func (cw *cappedWriter) Close() error {
71+
return cw.w.Close()
72+
}
73+
4174
// ExecSync executes a command in the container, and returns the stdout output.
4275
// If command exits with a non-zero exit code, an error is returned.
4376
func (c *criService) ExecSync(ctx context.Context, r *runtime.ExecSyncRequest) (*runtime.ExecSyncResponse, error) {
77+
const maxStreamSize = 1024 * 1024 * 16
78+
4479
var stdout, stderr bytes.Buffer
4580
exitCode, err := c.execInContainer(ctx, r.GetContainerId(), execOptions{
4681
cmd: r.GetCmd(),
47-
stdout: cioutil.NewNopWriteCloser(&stdout),
48-
stderr: cioutil.NewNopWriteCloser(&stderr),
82+
stdout: &cappedWriter{w: cioutil.NewNopWriteCloser(&stdout), remain: maxStreamSize},
83+
stderr: &cappedWriter{w: cioutil.NewNopWriteCloser(&stderr), remain: maxStreamSize},
4984
timeout: time.Duration(r.GetTimeout()) * time.Second,
5085
})
5186
if err != nil {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package server
18+
19+
import (
20+
"bytes"
21+
"testing"
22+
23+
cioutil "github.com/containerd/containerd/pkg/ioutil"
24+
"github.com/stretchr/testify/assert"
25+
)
26+
27+
func TestCWWrite(t *testing.T) {
28+
var buf bytes.Buffer
29+
cw := &cappedWriter{w: cioutil.NewNopWriteCloser(&buf), remain: 10}
30+
31+
n, err := cw.Write([]byte("hello"))
32+
assert.NoError(t, err)
33+
assert.Equal(t, 5, n)
34+
35+
n, err = cw.Write([]byte("helloworld"))
36+
assert.Equal(t, []byte("hellohello"), buf.Bytes(), "partial write")
37+
assert.Equal(t, 5, n)
38+
assert.ErrorIs(t, err, errNoRemain)
39+
40+
_, err = cw.Write([]byte("world"))
41+
assert.ErrorIs(t, err, errNoRemain)
42+
}
43+
44+
func TestCWClose(t *testing.T) {
45+
var buf bytes.Buffer
46+
cw := &cappedWriter{w: cioutil.NewNopWriteCloser(&buf), remain: 5}
47+
err := cw.Close()
48+
assert.NoError(t, err)
49+
}

0 commit comments

Comments
 (0)