Validate ControlMessageHeader.Length

Reported-by: syzbot+fee12cabc732cf92d9e7@syzkaller.appspotmail.com
PiperOrigin-RevId: 408776291
This commit is contained in:
Andrei Vagin
2021-11-09 20:58:59 -08:00
committed by gVisor bot
parent 5a65189253
commit 37792ee1e6
3 changed files with 23 additions and 1 deletions
+1
View File
@@ -37,6 +37,7 @@ go_test(
deps = [
"//pkg/abi/linux",
"//pkg/binary",
"//pkg/errors/linuxerr",
"//pkg/hostarch",
"//pkg/sentry/socket",
"@com_github_google_go_cmp//cmp:go_default_library",
+1 -1
View File
@@ -503,7 +503,7 @@ func Parse(t *kernel.Task, socketOrEndpoint interface{}, buf []byte, width uint)
}
length := int(h.Length) - linux.SizeOfControlMessageHeader
if length > len(buf) {
if length < 0 || length > len(buf) {
return socket.ControlMessages{}, linuxerr.EINVAL
}
+21
View File
@@ -22,6 +22,7 @@ import (
"github.com/google/go-cmp/cmp"
"gvisor.dev/gvisor/pkg/abi/linux"
"gvisor.dev/gvisor/pkg/binary"
"gvisor.dev/gvisor/pkg/errors/linuxerr"
"gvisor.dev/gvisor/pkg/hostarch"
"gvisor.dev/gvisor/pkg/sentry/socket"
)
@@ -57,3 +58,23 @@ func TestParse(t *testing.T) {
t.Errorf("unexpected message parsed, (-want, +got):\n%s", diff)
}
}
func TestParseRightsNegativeLength(t *testing.T) {
// Craft the control message to parse.
length := uint64(linux.SizeOfControlMessageHeader) + 128
hdr := linux.ControlMessageHeader{
Length: uint64(0xffffffff8f000000),
Level: linux.SOL_SOCKET,
Type: linux.SCM_RIGHTS,
}
hdrBuf := make([]byte, 0, length)
hdrBuf = binary.Marshal(hdrBuf, hostarch.ByteOrder, &hdr)
buf := make([]byte, length)
copy(buf, hdrBuf)
cmsg, err := Parse(nil, nil, buf, 8 /* width */)
if err != linuxerr.EINVAL {
t.Fatalf("Parse(_, _, %+v, _): %v", cmsg, err)
}
}