From 9889c29d6d26ba86b5e3590eac85bfb8393dd54e Mon Sep 17 00:00:00 2001 From: Fabricio Voznika Date: Tue, 15 May 2018 14:38:32 -0700 Subject: Fix problem with sendfile(2) writing less data When the amount of data read is more than the amount written, sendfile would not adjust 'in file' position and would resume from the wrong location. Closes #33 PiperOrigin-RevId: 196731287 Change-Id: Ia219895dd765016ed9e571fd5b366963c99afb27 --- pkg/sentry/syscalls/linux/sys_file.go | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'pkg/sentry/syscalls/linux') diff --git a/pkg/sentry/syscalls/linux/sys_file.go b/pkg/sentry/syscalls/linux/sys_file.go index 5fbacc15e..1d61ac9f0 100644 --- a/pkg/sentry/syscalls/linux/sys_file.go +++ b/pkg/sentry/syscalls/linux/sys_file.go @@ -1929,8 +1929,16 @@ func Sendfile(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.Sysc // If we don't have a provided offset. } else { // Send data using readv. + inOff := inFile.Offset() r := &io.LimitedReader{R: &fs.FileReader{t, inFile}, N: count} n, err = io.Copy(w, r) + inOff += n + if inFile.Offset() != inOff { + // Adjust file position in case more bytes were read than written. + if _, err := inFile.Seek(t, fs.SeekSet, inOff); err != nil { + return 0, nil, syserror.EIO + } + } } // We can only pass a single file to handleIOError, so pick inFile -- cgit v1.2.3