Skip to content

Commit 943588b

Browse files
Kazuyoshi Katodmcgowan
authored andcommitted
[release/1.5] Limit the response size of ExecSync
Signed-off-by: Kazuyoshi Kato <[email protected]> (cherry picked from commit 49ca87d) Signed-off-by: Kazuyoshi Kato <[email protected]>
1 parent a4014bc commit 943588b

2 files changed

Lines changed: 85 additions & 2 deletions

File tree

pkg/cri/server/container_execsync.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,48 @@ import (
3838
cioutil "github.com/containerd/containerd/pkg/ioutil"
3939
)
4040

41+
type cappedWriter struct {
42+
w io.WriteCloser
43+
remain int
44+
}
45+
46+
var errNoRemain = errors.New("no more space to write")
47+
48+
func (cw *cappedWriter) Write(p []byte) (int, error) {
49+
if cw.remain <= 0 {
50+
return 0, errNoRemain
51+
}
52+
53+
end := cw.remain
54+
if end > len(p) {
55+
end = len(p)
56+
}
57+
written, err := cw.w.Write(p[0:end])
58+
cw.remain -= written
59+
60+
if err != nil {
61+
return written, err
62+
}
63+
if written < len(p) {
64+
return written, errNoRemain
65+
}
66+
return written, nil
67+
}
68+
69+
func (cw *cappedWriter) Close() error {
70+
return cw.w.Close()
71+
}
72+
4173
// ExecSync executes a command in the container, and returns the stdout output.
4274
// If command exits with a non-zero exit code, an error is returned.
4375
func (c *criService) ExecSync(ctx context.Context, r *runtime.ExecSyncRequest) (*runtime.ExecSyncResponse, error) {
76+
const maxStreamSize = 1024 * 1024 * 16
77+
4478
var stdout, stderr bytes.Buffer
4579
exitCode, err := c.execInContainer(ctx, r.GetContainerId(), execOptions{
4680
cmd: r.GetCmd(),
47-
stdout: cioutil.NewNopWriteCloser(&stdout),
48-
stderr: cioutil.NewNopWriteCloser(&stderr),
81+
stdout: &cappedWriter{w: cioutil.NewNopWriteCloser(&stdout), remain: maxStreamSize},
82+
stderr: &cappedWriter{w: cioutil.NewNopWriteCloser(&stderr), remain: maxStreamSize},
4983
timeout: time.Duration(r.GetTimeout()) * time.Second,
5084
})
5185
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)