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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
|
// Copyright 2015 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 sys
const ptrSize = 8
type Call struct {
ID int
NR int // kernel syscall number
CallID int
Name string
CallName string
Args []Type
Ret Type
}
type Type interface {
Name() string
Optional() bool
Default() uintptr
Size() uintptr
Align() uintptr
}
func IsPad(t Type) bool {
if ct, ok := t.(ConstType); ok && ct.IsPad {
return true
}
return false
}
type TypeCommon struct {
TypeName string
IsOptional bool
}
func (t TypeCommon) Name() string {
return t.TypeName
}
func (t TypeCommon) Optional() bool {
return t.IsOptional
}
func (t TypeCommon) Default() uintptr {
return 0
}
type (
ResourceKind int
ResourceSubkind int
)
const (
ResFD ResourceKind = iota
ResIOCtx
ResIPC
ResKey
ResInotifyDesc
ResPid
ResUid
ResGid
ResTimerid
ResIocbPtr
ResDrmCtx
)
const (
ResAny ResourceSubkind = iota
FdFile
FdSock
FdPipe
FdSignal
FdEvent
FdTimer
FdEpoll
FdDir
FdMq
FdInotify
FdFanotify
FdTty
FdDRI
FdFuse
FdKdbus
FdBpfMap
FdBpfProg
FdPerf
FdUserFault
FdAlg
FdAlgConn
FdNfcRaw
FdNfcLlcp
FdBtHci
FdBtSco
FdBtL2cap
FdBtRfcomm
FdBtHidp
FdBtCmtp
FdBtBnep
FdUnix
FdSctp
FdNetlink
FdKvm
FdKvmVm
FdKvmCpu
FdSndSeq
FdSndTimer
FdSndControl
FdInputEvent
IPCMsq
IPCSem
IPCShm
)
func ResourceKinds() []ResourceKind {
return []ResourceKind{
ResFD,
ResIOCtx,
ResIPC,
ResKey,
ResInotifyDesc,
ResPid,
ResUid,
ResGid,
ResTimerid,
ResIocbPtr,
}
}
func ResourceSubkinds(kind ResourceKind) []ResourceSubkind {
switch kind {
case ResFD:
return []ResourceSubkind{ResAny, FdFile, FdSock, FdPipe, FdSignal, FdEvent,
FdTimer, FdEpoll, FdDir, FdMq, FdInotify, FdFanotify, FdTty,
FdDRI, FdFuse, FdKdbus, FdBpfMap, FdBpfProg, FdPerf, FdUserFault,
FdAlg, FdAlgConn, FdNfcRaw, FdNfcLlcp, FdBtHci, FdBtSco, FdBtL2cap,
FdBtRfcomm, FdBtHidp, FdBtCmtp, FdBtBnep, FdUnix, FdSctp, FdNetlink, FdKvm, FdKvmVm,
FdKvmCpu, FdSndSeq, FdSndTimer, FdSndControl, FdInputEvent}
case ResIPC:
return []ResourceSubkind{IPCMsq, IPCSem, IPCShm}
case ResIOCtx, ResKey, ResInotifyDesc, ResPid, ResUid, ResGid, ResTimerid, ResIocbPtr, ResDrmCtx:
return []ResourceSubkind{ResAny}
default:
panic("unknown resource kind")
}
}
func SocketSubkinds() []ResourceSubkind {
return []ResourceSubkind{FdAlg, FdAlgConn, FdNfcRaw, FdNfcLlcp, FdBtHci, FdBtSco,
FdBtL2cap, FdBtRfcomm, FdBtHidp, FdBtCmtp, FdBtBnep, FdUnix, FdSctp, FdNetlink}
}
const (
InvalidFD = ^uintptr(0)
BogusFD = uintptr(100000 - 1)
)
type ResourceType struct {
TypeCommon
Kind ResourceKind
Subkind ResourceSubkind
}
func (t ResourceType) Default() uintptr {
switch t.Kind {
case ResFD:
return InvalidFD
case ResIOCtx:
return 0
case ResIPC:
return 0
case ResKey:
return 0
case ResInotifyDesc:
return 0
case ResPid:
return 0
case ResUid:
return 0
case ResGid:
return 0
case ResTimerid:
return 0
case ResDrmCtx:
return 0
default:
panic("unknown resource type")
}
}
func (t ResourceType) SpecialValues() []uintptr {
switch t.Kind {
case ResFD:
return []uintptr{InvalidFD, BogusFD, ^uintptr(0) - 99 /*AT_FDCWD*/}
case ResIOCtx:
return []uintptr{0}
case ResIPC:
return []uintptr{0, ^uintptr(0)}
case ResKey:
// KEY_SPEC_THREAD_KEYRING values
return []uintptr{0, ^uintptr(0), ^uintptr(0) - 1, ^uintptr(0) - 2, ^uintptr(0) - 3,
^uintptr(0) - 4, ^uintptr(0) - 5, ^uintptr(0) - 6, ^uintptr(0) - 7}
case ResInotifyDesc:
return []uintptr{0}
case ResPid:
return []uintptr{0, ^uintptr(0)}
case ResUid:
return []uintptr{0, ^uintptr(0)}
case ResGid:
return []uintptr{0, ^uintptr(0)}
case ResTimerid:
return []uintptr{0}
case ResDrmCtx:
return []uintptr{0}
default:
panic("unknown resource kind")
}
}
func (t ResourceType) Size() uintptr {
switch t.Kind {
case ResFD:
return 4
case ResIOCtx:
return 8
case ResIPC:
return 4
case ResKey:
return 4
case ResInotifyDesc:
return 4
case ResPid:
return 4
case ResUid:
return 4
case ResGid:
return 4
case ResTimerid:
return 4
case ResDrmCtx:
return 4
default:
panic("unknown resource kind")
}
}
func (t ResourceType) Align() uintptr {
return t.Size()
}
func (t ResourceType) SubKinds() []ResourceSubkind {
return ResourceSubkinds(t.Kind)
}
type FileoffType struct {
TypeCommon
TypeSize uintptr
File string
}
func (t FileoffType) Size() uintptr {
return t.TypeSize
}
func (t FileoffType) Align() uintptr {
return t.Size()
}
type BufferKind int
const (
BufferBlob BufferKind = iota
BufferString
BufferSockaddr
BufferFilesystem
BufferAlgType
BufferAlgName
)
type BufferType struct {
TypeCommon
Kind BufferKind
}
func (t BufferType) Size() uintptr {
switch t.Kind {
case BufferAlgType:
return 14
case BufferAlgName:
return 64
default:
panic("buffer size is not statically known")
}
}
func (t BufferType) Align() uintptr {
return 1
}
type VmaType struct {
TypeCommon
}
func (t VmaType) Size() uintptr {
return ptrSize
}
func (t VmaType) Align() uintptr {
return t.Size()
}
type LenType struct {
TypeCommon
TypeSize uintptr
ByteSize bool // want size in bytes instead of array size
Buf string
}
func (t LenType) Size() uintptr {
return t.TypeSize
}
func (t LenType) Align() uintptr {
return t.Size()
}
type FlagsType struct {
TypeCommon
TypeSize uintptr
Vals []uintptr
}
func (t FlagsType) Size() uintptr {
return t.TypeSize
}
func (t FlagsType) Align() uintptr {
return t.Size()
}
type ConstType struct {
TypeCommon
TypeSize uintptr
Val uintptr
IsPad bool
}
func (t ConstType) Size() uintptr {
return t.TypeSize
}
func (t ConstType) Align() uintptr {
return t.Size()
}
type StrConstType struct {
TypeCommon
TypeSize uintptr
Val string
}
func (t StrConstType) Size() uintptr {
return ptrSize
}
func (t StrConstType) Align() uintptr {
return t.Size()
}
type IntKind int
const (
IntPlain IntKind = iota
IntSignalno
IntInaddr
IntInport
)
type IntType struct {
TypeCommon
TypeSize uintptr
Kind IntKind
}
func (t IntType) Size() uintptr {
return t.TypeSize
}
func (t IntType) Align() uintptr {
return t.Size()
}
type FilenameType struct {
TypeCommon
}
func (t FilenameType) Size() uintptr {
panic("filename size is not statically known")
}
func (t FilenameType) Align() uintptr {
return 1
}
type ArrayType struct {
TypeCommon
Type Type
Len uintptr // 0 if variable-length, unused for now
}
func (t ArrayType) Size() uintptr {
if t.Len == 0 {
return 0 // for trailing embed arrays
}
return t.Len * t.Type.Size()
}
func (t ArrayType) Align() uintptr {
return t.Type.Align()
}
type PtrType struct {
TypeCommon
Type Type
Dir Dir
}
func (t PtrType) Size() uintptr {
return ptrSize
}
func (t PtrType) Align() uintptr {
return t.Size()
}
type StructType struct {
TypeCommon
Fields []Type
padded bool
packed bool
align uintptr
}
func (t StructType) Size() uintptr {
if !t.padded {
panic("struct is not padded yet")
}
var size uintptr
for _, f := range t.Fields {
size += f.Size()
}
return size
}
func (t StructType) Align() uintptr {
if t.align != 0 {
return t.align // overrided by user attribute
}
var align uintptr
for _, f := range t.Fields {
if a1 := f.Align(); align < a1 {
align = a1
}
}
return align
}
type UnionType struct {
TypeCommon
Options []Type
varlen bool
}
func (t UnionType) Size() uintptr {
if t.varlen {
panic("union size is not statically known")
}
size := t.Options[0].Size()
for _, opt := range t.Options {
if size < opt.Size() {
size = opt.Size()
}
}
return size
}
func (t UnionType) Align() uintptr {
var align uintptr
for _, opt := range t.Options {
if a1 := opt.Align(); align < a1 {
align = a1
}
}
return align
}
type Dir int
const (
DirIn Dir = iota
DirOut
DirInOut
)
var ctors = make(map[ResourceKind]map[ResourceSubkind][]*Call)
// ResourceConstructors returns a list of calls that can create a resource of the given kind/subkind.
func ResourceConstructors(kind ResourceKind, sk ResourceSubkind) []*Call {
return ctors[kind][sk]
}
func initResources() {
for _, kind := range ResourceKinds() {
ctors[kind] = make(map[ResourceSubkind][]*Call)
for _, sk := range ResourceSubkinds(kind) {
ctors[kind][sk] = ResourceCtors(kind, sk, false)
}
}
}
func ResourceCtors(kind ResourceKind, sk ResourceSubkind, precise bool) []*Call {
// Find calls that produce the necessary resources.
var metas []*Call
// Recurse into arguments to see if there is an out/inout arg of necessary type.
var checkArg func(typ Type, dir Dir) bool
checkArg = func(typ Type, dir Dir) bool {
if resarg, ok := typ.(ResourceType); ok && dir != DirIn && resarg.Kind == kind &&
(sk == resarg.Subkind || sk == ResAny || (resarg.Subkind == ResAny && !precise)) {
return true
}
switch typ1 := typ.(type) {
case ArrayType:
if checkArg(typ1.Type, dir) {
return true
}
case StructType:
for _, fld := range typ1.Fields {
if checkArg(fld, dir) {
return true
}
}
case UnionType:
for _, opt := range typ1.Options {
if checkArg(opt, dir) {
return true
}
}
case PtrType:
if checkArg(typ1.Type, typ1.Dir) {
return true
}
}
return false
}
for _, meta := range Calls {
ok := false
for _, arg := range meta.Args {
if checkArg(arg, DirIn) {
ok = true
break
}
}
if !ok && meta.Ret != nil && checkArg(meta.Ret, DirOut) {
ok = true
}
if ok {
metas = append(metas, meta)
}
}
return metas
}
func (c *Call) InputResources() []ResourceType {
var resources []ResourceType
var checkArg func(typ Type, dir Dir)
checkArg = func(typ Type, dir Dir) {
switch typ1 := typ.(type) {
case ResourceType:
if dir != DirOut && !typ1.IsOptional {
resources = append(resources, typ1)
}
case ArrayType:
checkArg(typ1.Type, dir)
case PtrType:
checkArg(typ1.Type, typ1.Dir)
case StructType:
for _, fld := range typ1.Fields {
checkArg(fld, dir)
}
case UnionType:
for _, opt := range typ1.Options {
checkArg(opt, dir)
}
}
}
for _, arg := range c.Args {
checkArg(arg, DirIn)
}
return resources
}
func TransitivelyEnabledCalls(enabled map[*Call]bool) map[*Call]bool {
supported := make(map[*Call]bool)
for c := range enabled {
supported[c] = true
}
for {
n := len(supported)
for c := range enabled {
if !supported[c] {
continue
}
canCreate := true
for _, res := range c.InputResources() {
noctors := true
for _, ctor := range ResourceCtors(res.Kind, res.Subkind, true) {
if supported[ctor] {
noctors = false
break
}
}
if noctors {
canCreate = false
break
}
}
if !canCreate {
delete(supported, c)
}
}
if n == len(supported) {
break
}
}
return supported
}
var (
CallCount int
CallMap = make(map[string]*Call)
CallID = make(map[string]int)
)
func init() {
initCalls()
initResources()
initAlign()
for _, c := range Calls {
c.NR = numbers[c.ID]
}
for _, c := range Calls {
if CallMap[c.Name] != nil {
println(c.Name)
panic("duplicate syscall")
}
id, ok := CallID[c.CallName]
if !ok {
id = len(CallID)
CallID[c.CallName] = id
}
c.CallID = id
CallMap[c.Name] = c
}
CallCount = len(CallID)
}
|