sendfile: limit a buffer size

When sendfile is called for two non-pipe file descriptors, we use a buffer to
read data from one fd and write it to another one.  The buffer size has to be
limited to avoid large memory allocations and long delays. In Linux, the buffer
size is limited by a size of an internl pipe. Here, we repeat this behavior.

Reported-by: syzbot+d82dbadde2cbe70eb6dd@syzkaller.appspotmail.com
PiperOrigin-RevId: 429484781
This commit is contained in:
Andrei Vagin
2022-02-17 22:41:14 -08:00
committed by gVisor bot
parent dfcf798425
commit b413d78c27
+13 -2
View File
@@ -380,8 +380,20 @@ func Sendfile(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.Sysc
}
} else {
// Read inFile to buffer, then write the contents to outFile.
buf := make([]byte, count)
//
// The buffer size has to be limited to avoid large memory
// allocations and long delays. In Linux, the buffer size is
// limited by a size of an internl pipe. Here, we repeat this
// behavior.
bufSize := count
if bufSize > pipe.MaximumPipeSize {
bufSize = pipe.MaximumPipeSize
}
buf := make([]byte, bufSize)
for {
if int64(len(buf)) > count-total {
buf = buf[:count-total]
}
var readN int64
if offset != -1 {
readN, err = inFile.PRead(t, usermem.BytesIOSequence(buf), offset, vfs.ReadOptions{})
@@ -423,7 +435,6 @@ func Sendfile(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.Sysc
}
total += readN
buf = buf[readN:]
if total == count {
break
}