From 1808de66ce1b8db0fc76b5b6398e3386ca2e7ad6 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Tue, 28 Nov 2017 16:25:45 +0100 Subject: prog: repair arrays/buffers with incorrect size in Deserialize For string[N] we successfully deserialize a string of any length. Similarly for a fixed-size array[T, N] we successfully deserialize an array of any size. Such programs later crash in foreachSubargOffset because static size Type.Size() does not match what we've calculated iterating over fields. The crash happens only in SerializeForExec in syz-fuzzer, which is especially bad. Fix this from both sides: 1. Validate sizes of arrays/buffers in Validate. 2. Repair incorrect sizes in Deserialize. --- prog/encoding.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'prog/encoding.go') diff --git a/prog/encoding.go b/prog/encoding.go index 936e896a1..95e312662 100644 --- a/prog/encoding.go +++ b/prog/encoding.go @@ -287,6 +287,12 @@ func (target *Target) parseArg(typ Type, p *parser, vars map[string]Arg) (Arg, e if err != nil { return nil, fmt.Errorf("data arg has bad value '%v'", val) } + if !typ.Varlen() { + if diff := int(typ.Size()) - len(data); diff > 0 { + data = append(data, make([]byte, diff)...) + } + data = data[:typ.Size()] + } arg = MakeDataArg(typ, data) case '{': t1, ok := typ.(*StructType) @@ -336,6 +342,12 @@ func (target *Target) parseArg(typ Type, p *parser, vars map[string]Arg) (Arg, e } } p.Parse(']') + if t1.Kind == ArrayRangeLen && t1.RangeBegin == t1.RangeEnd { + for uint64(len(inner)) < t1.RangeBegin { + inner = append(inner, defaultArg(t1.Type)) + } + inner = inner[:t1.RangeBegin] + } arg = MakeGroupArg(typ, inner) case '@': t1, ok := typ.(*UnionType) -- cgit mrf-deployment