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
|
// Copyright 2020 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 powerpc
import (
"math/rand"
"github.com/google/syzkaller/pkg/ifuzz/iset"
)
const (
// Valid hcall humbers at the momemt are: 4..0x450.
MaxHcall = 0x450 // MAX_HCALL
SprnSrr0 = 0x01A // pc for rfid (SPRN_SRR0)
SprnSrr1 = 0x01B // msr for rfid (SPRN_SRR1)
)
// nolint:dupl
func (insnset *InsnSet) initPseudo() {
insnset.Insns = append(insnset.Insns, &Insn{
Name: "PSEUDO_hypercall",
Priv: true,
Pseudo: true,
generator: func(cfg *iset.Config, r *rand.Rand) []byte {
gen := makeGen(insnset, cfg, r)
gen.sc(1)
return gen.text
},
})
insnset.Insns = append(insnset.Insns, &Insn{
Name: "PSEUDO_syscall",
Priv: true,
Pseudo: true,
generator: func(cfg *iset.Config, r *rand.Rand) []byte {
gen := makeGen(insnset, cfg, r)
gen.sc(0)
return gen.text
},
})
insnset.Insns = append(insnset.Insns, &Insn{
Name: "PSEUDO_ultracall",
Priv: true,
Pseudo: true,
generator: func(cfg *iset.Config, r *rand.Rand) []byte {
gen := makeGen(insnset, cfg, r)
gen.sc(2)
return gen.text
},
})
insnset.Insns = append(insnset.Insns, &Insn{
Name: "PSEUDO_rtas",
Priv: true,
Pseudo: true,
generator: func(cfg *iset.Config, r *rand.Rand) []byte {
gen := makeGen(insnset, cfg, r)
gen.rtas()
return gen.text
},
})
insnset.Insns = append(insnset.Insns, &Insn{
Name: "PSEUDO_rfid",
Priv: true,
Pseudo: true,
generator: func(cfg *iset.Config, r *rand.Rand) []byte {
gen := makeGen(insnset, cfg, r)
gen.rfid()
return gen.text
},
})
}
type generator struct {
imap insnSetMap
cfg *iset.Config
r *rand.Rand
text []byte
}
func makeGen(insnset *InsnSet, cfg *iset.Config, r *rand.Rand) *generator {
return &generator{
imap: insnset.insnMap,
cfg: cfg,
r: r,
}
}
func (gen *generator) byte(v []byte) {
gen.text = append(gen.text, v...)
}
func (gen *generator) sc(lev uint) {
imap := gen.imap
n := gen.r.Intn(9)
hcrange := gen.r.Intn(3)
offset := 4
maxhc := MaxHcall
switch hcrange {
case 1:
offset = 0xf000
maxhc = 0xf810
case 2:
offset = 0xef00
maxhc = 0xef20
}
hc := gen.r.Intn((maxhc-offset)/4)*4 + offset
gen.byte(imap.ld64(3, uint64(hc)))
for i := 4; i < n+4; i++ {
gen.byte(imap.ld64(uint(i), gen.r.Uint64()))
}
gen.byte(imap.sc(lev))
}
func (gen *generator) rtas() {
imap := gen.imap
addr := iset.GenerateInt(gen.cfg, gen.r, 8)
token := uint32(gen.r.Intn(8) << 24) // There are only 4 tokens handled by KVM and it is BigEndian.
reg := uint(iset.GenerateInt(gen.cfg, gen.r, 4))
gen.byte(imap.ldgpr32(reg, reg+uint(1), addr, token))
for i := 0; i < gen.r.Intn(4)+1; i++ {
gen.byte(imap.ldgpr32(reg, reg+uint(1), addr+uint64(i*4),
uint32(iset.GenerateInt(gen.cfg, gen.r, 4))))
}
gen.byte(imap.ld64(3, 0xF000)) // 0xF000 is a custom H_RTAS hypercall
gen.byte(imap.ld64(4, addr))
gen.byte(imap.sc(1))
}
func (gen *generator) rfid() {
imap := gen.imap
tmpreg := uint(gen.r.Intn(32))
// SRR0 contains a PC
gen.byte(imap.ld64(tmpreg, iset.GenerateInt(gen.cfg, gen.r, 8)))
gen.byte(imap["mtspr"].enc(map[string]uint{"RS": tmpreg, "SPR": SprnSrr0}))
// SRR1 contains an MSR
gen.byte(imap.ld64(tmpreg, gen.r.Uint64()))
gen.byte(imap["mtspr"].enc(map[string]uint{"RS": tmpreg, "SPR": SprnSrr1}))
gen.byte(imap["rfid"].enc(map[string]uint{}))
}
|