Debian Codegraph @

zstd/seqdec_oob_test.go

raw
1 // Copyright 2019+ Klaus Post. All rights reserved.
2 // License information can be found in the LICENSE file.
3
4 //go:build (amd64 || arm64) && !appengine && !noasm && gc
5
6 package zstd
7
8 import (
9 "bytes"
10 "io"
11 "testing"
12 )
13
14 // TestDecodeSyncUnsafeOOB reproduces an out-of-bounds write in the unsafe
15 // (extended-copy) decodeSync assembly. The per-sequence space check requires
16 // only that outPos+ll+ml <= cap(s.out), but the unsafe copies write in 16-byte
17 // blocks and overrun the logical end by up to compressedBlockOverAlloc-1 bytes.
18 // A malformed stream whose declared content size (maxSyncLen) is a few bytes
19 // below what its sequences actually produce lets the decode end land inside the
20 // 16-byte over-allocation slack — the space check still passes, but the final
21 // block copy then writes past cap(s.out).
22 //
23 // The overrun is at most 15 bytes and Go's size-class rounding usually hides it
24 // from -race and even -asan (the slop is inside the same size class), so this
25 // test detects it directly: s.out is carved from a larger backing array whose
26 // tail is filled with a canary and checked after the decode.
27 func TestDecodeSyncUnsafeOOB(t *testing.T) {
28 const canaryByte = 0x5a
29 const canaryLen = 64
30
31 zr := testCreateZipReader("testdata/seqs.zip", t)
32 var asmPaths int
33 for _, tt := range zr.File {
34 var ref testSequence
35 if !ref.parse(tt.Name) {
36 continue
37 }
38 r, err := tt.Open()
39 if err != nil {
40 t.Fatal(err)
41 }
42 seqData, err := io.ReadAll(r)
43 if err != nil {
44 t.Fatal(err)
45 }
46 buf := bytes.NewBuffer(seqData)
47 s := readDecoders(t, buf, ref)
48 lits := s.literals
49 hist := make([]byte, ref.win)
50
51 reinit := func() {
52 if err := s.br.init(buf.Bytes()); err != nil {
53 t.Fatal(err)
54 }
55 if err := s.litLengths.init(s.br); err != nil {
56 t.Fatal(err)
57 }
58 if err := s.offsets.init(s.br); err != nil {
59 t.Fatal(err)
60 }
61 if err := s.matchLengths.init(s.br); err != nil {
62 t.Fatal(err)
63 }
64 s.literals = lits
65 s.prevOffset = ref.prevOffsets
66 }
67
68 // Learn the real decoded size N via the generic path. With cap(out)==0
69 // and maxSyncLen==0, decodeSyncSimple reports unsupported and decodeSync
70 // falls back to the generic loop, which appends the full output.
71 reinit()
72 s.out = nil
73 s.maxSyncLen = 0
74 if err := s.decodeSync(hist); err != nil {
75 continue // not sync-decodable; skip
76 }
77 n := len(s.out)
78 if n == 0 {
79 continue
80 }
81
82 // Model a malformed stream that under-reports its size by k bytes, so the
83 // decode ends inside the over-allocation slack. k in [2,15] keeps the
84 // output within cap while pushing the final copy's overrun past it.
85 for k := 2; k <= 15; k++ {
86 maxSyncLen := n - k
87 if maxSyncLen <= 0 {
88 continue
89 }
90 capOut := maxSyncLen + compressedBlockOverAlloc
91 backing := make([]byte, capOut+canaryLen)
92 for i := range backing {
93 backing[i] = canaryByte
94 }
95 reinit()
96 s.out = backing[:0:capOut]
97 s.maxSyncLen = uint64(maxSyncLen)
98
99 // The whole point of this test is the unsafe (extended-copy)
100 // variant; fail loudly if the buffer geometry would make
101 // decodeSyncSimple select the safe copies instead.
102 if s.useSafeDecodeSync() {
103 t.Fatalf("%s k=%d: buffer sizing selects the safe copy variant; the unsafe path is not exercised",
104 tt.Name, k)
105 }
106
107 // A well-formed decode into this buffer must either succeed or fail
108 // cleanly; either way it must never write past cap(s.out).
109 supported, _ := s.decodeSyncSimple(hist)
110 if !supported {
111 continue
112 }
113 asmPaths++
114 for i := capOut; i < len(backing); i++ {
115 if backing[i] != canaryByte {
116 t.Fatalf("%s k=%d: unsafe decodeSync wrote past cap(out)=%d (first corrupted byte at +%d)",
117 tt.Name, k, capOut, i-capOut)
118 }
119 }
120 }
121 }
122 if asmPaths == 0 {
123 t.Fatal("test did not exercise the unsafe decodeSync asm path")
124 }
125 }