1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
// Copyright 2021 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package prog
import (
"math/rand"
"testing"
)
func TestAssignRandomAsync(t *testing.T) {
tests := []struct {
os string
arch string
orig string
check func(*Prog) bool
}{
{
"linux", "amd64",
`r0 = openat(0xffffffffffffff9c, &AUTO='./file1\x00', 0x42, 0x1ff)
write(r0, &AUTO="01010101", 0x4)
read(r0, &AUTO=""/4, 0x4)
close(r0)
`,
func(p *Prog) bool {
return !p.Calls[0].Props.Async
},
},
{
"linux", "amd64",
`r0 = openat(0xffffffffffffff9c, &AUTO='./file1\x00', 0x42, 0x1ff)
nanosleep(&AUTO={0x0,0x4C4B40}, &AUTO={0,0})
write(r0, &AUTO="01010101", 0x4)
read(r0, &AUTO=""/4, 0x4)
close(r0)
`,
func(p *Prog) bool {
return !p.Calls[0].Props.Async || !p.Calls[1].Props.Async
},
},
{
"linux", "amd64",
`r0 = openat(0xffffffffffffff9c, &AUTO='./file1\x00', 0x42, 0x1ff)
r1 = dup(r0)
r2 = dup(r1)
r3 = dup(r2)
r4 = dup(r3)
`,
func(p *Prog) bool {
for _, call := range p.Calls[0 : len(p.Calls)-1] {
if call.Props.Async {
return false
}
}
return true
},
},
}
_, rs, iters := initTest(t)
r := rand.New(rs)
anyAsync := false
for _, test := range tests {
target, err := GetTarget(test.os, test.arch)
if err != nil {
t.Fatal(err)
}
p, err := target.Deserialize([]byte(test.orig), Strict)
if err != nil {
t.Fatal(err)
}
for i := 0; i < iters; i++ {
collided := AssignRandomAsync(p, r)
if !test.check(collided) {
t.Fatalf("bad async assignment:\n%s\n", collided.Serialize())
}
for _, call := range collided.Calls {
anyAsync = anyAsync || call.Props.Async
}
}
}
if !anyAsync {
t.Fatalf("not a single async was assigned")
}
}
func TestDoubleExecCollide(t *testing.T) {
tests := []struct {
os string
arch string
orig string
duplicated string
shouldFail bool
}{
{
"linux", "amd64",
`r0 = openat(0xffffffffffffff9c, &AUTO='./file1\x00', 0x42, 0x1ff)
r1 = dup(r0)
r2 = dup(r1)
r3 = dup(r2)
r4 = dup(r2)
r5 = dup(r3)
`,
`r0 = openat(0xffffffffffffff9c, &(0x7f0000000040)='./file1\x00', 0x42, 0x1ff)
r1 = dup(r0)
r2 = dup(r1)
r3 = dup(r2)
dup(r2)
dup(r3)
openat(0xffffffffffffff9c, &(0x7f0000000040)='./file1\x00', 0x42, 0x1ff)
dup(r0)
dup(r1)
dup(r2)
dup(r2)
dup(r3)
`,
false,
},
}
_, rs, iters := initTest(t)
r := rand.New(rs)
for _, test := range tests {
target, err := GetTarget(test.os, test.arch)
if err != nil {
t.Fatal(err)
}
p, err := target.Deserialize([]byte(test.orig), Strict)
if err != nil {
t.Fatal(err)
}
for i := 0; i < iters; i++ {
collided, err := DoubleExecCollide(p, r)
if test.shouldFail && err == nil {
t.Fatalf("expected to fail, but it hasn't")
} else if !test.shouldFail && err != nil {
t.Fatalf("unexpected error: %s", err)
}
if test.duplicated != "" {
woProps := collided.Clone()
for _, c := range woProps.Calls {
c.Props = CallProps{}
}
serialized := string(woProps.Serialize())
if serialized != test.duplicated {
t.Fatalf("expected:%s\ngot:%s\n", test.duplicated, serialized)
}
}
// TODO: also test the `async` assignment.
}
}
}
|