-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontext_generic.go
585 lines (488 loc) · 14.9 KB
/
context_generic.go
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
package mimic
import (
"encoding/base64"
"encoding/binary"
"encoding/json"
"fmt"
"strings"
)
func unmarshalGeneric(name string, ctx json.RawMessage) (Context, error) {
var result GenericContext
err := json.Unmarshal(ctx, &result)
if err != nil {
return nil, err
}
result.Name = name
return &result, nil
}
// GenericContextRegisters are registers which the generic context can set when loading
type GenericContextRegisters struct {
R1 string `json:"r1,omitempty"`
R2 string `json:"r2,omitempty"`
R3 string `json:"r3,omitempty"`
R4 string `json:"r4,omitempty"`
R5 string `json:"r5,omitempty"`
}
// GenericContext implements mimic.Context. The goal of GenericContext and its GenericContext... struct types is to
// provide a method to construct any type of eBPF context. Being generic also means we have to be very verbose which
// is not always desirable, it is recommended to use specific contexts whenever possible, but this type can always be
// used as a fallback in situations where no specific context type exists.
type GenericContext struct {
Name string `json:"-"`
Registers GenericContextRegisters `json:"registers"`
Memory []GenericContextMemory `json:"memory"`
EmulatorRaw map[string]json.RawMessage `json:"emulator"`
Emulator map[string]interface{} `json:"-"`
loaded bool
}
// MarshalJSON implements json.Marshaler
func (c *GenericContext) MarshalJSON() ([]byte, error) {
c.EmulatorRaw = make(map[string]json.RawMessage)
for k, v := range c.Emulator {
jsonV, err := json.Marshal(v)
if err != nil {
return nil, fmt.Errorf("marshal '%s': %w", k, err)
}
c.EmulatorRaw[k] = json.RawMessage(jsonV)
}
type Alias GenericContext
a := Alias(*c)
b, err := json.Marshal(a)
if err != nil {
return nil, err
}
proto := protoCtx{
Name: c.Name,
Type: "generic",
Ctx: b,
}
return json.Marshal(proto)
}
// GetName returns the name of the context
func (c *GenericContext) GetName() string {
return c.Name
}
// SetName sets the name of the context
func (c *GenericContext) SetName(name string) {
c.Name = name
}
// Load loads the context into a process. Load is called by the VM when creating a new process, users don't have to
// call this function manually. Loading a context into a process will register memory for the context at the memory
// controller of the VM.
func (c *GenericContext) Load(process *Process) error {
if c.loaded {
return fmt.Errorf("context is already loaded, please cleanup before re-loading")
}
regVal := func(memName string) (uint64, error) {
var mem *GenericContextMemory
for i, m := range c.Memory {
if m.Name == memName {
mem = &c.Memory[i]
break
}
}
if mem == nil {
return 0, fmt.Errorf("register refers to memory '%s' which doesn't exist", memName)
}
switch mem.Type {
case "block":
addr, err := mem.Block.GetAddr(process, mem)
if err != nil {
return 0, fmt.Errorf("block getaddr: %w", err)
}
return uint64(addr), nil
case "ptr":
addr, err := mem.Pointer.GetValue(process, c)
if err != nil {
return 0, fmt.Errorf("pointer getaddr: %w", err)
}
return uint64(addr), nil
case "int":
return uint64(mem.Int.Value), nil
case "struct":
addr, err := mem.Struct.GetAddr(process, c, mem)
if err != nil {
return 0, fmt.Errorf("struct getaddr: %w", err)
}
return uint64(addr), nil
}
return 0, fmt.Errorf("unknown memory type '%s'", mem.Type)
}
if c.Registers.R1 != "" {
val, err := regVal(c.Registers.R1)
if err != nil {
return fmt.Errorf("r1: %w", err)
}
process.Registers.R1 = val
}
if c.Registers.R2 != "" {
val, err := regVal(c.Registers.R2)
if err != nil {
return fmt.Errorf("r2: %w", err)
}
process.Registers.R2 = val
}
if c.Registers.R3 != "" {
val, err := regVal(c.Registers.R3)
if err != nil {
return fmt.Errorf("r3: %w", err)
}
process.Registers.R3 = val
}
if c.Registers.R4 != "" {
val, err := regVal(c.Registers.R4)
if err != nil {
return fmt.Errorf("r4: %w", err)
}
process.Registers.R4 = val
}
if c.Registers.R5 != "" {
val, err := regVal(c.Registers.R5)
if err != nil {
return fmt.Errorf("r5: %w", err)
}
process.Registers.R5 = val
}
return nil
}
// Cleanup cleans up the context, the process call this function the Process.Cleanup is called, users should not have to
// manually call this function. Cleaning up the context will remove the associated memory from the processes memory
// controller and make the context ready to be re-used/re-loaded.
func (c *GenericContext) Cleanup(process *Process) error {
c.loaded = false
for _, mem := range c.Memory {
switch mem.Type {
case "block":
err := mem.Block.Cleanup(process)
if err != nil {
return err
}
case "struct":
err := mem.Struct.Cleanup(process)
if err != nil {
return err
}
}
}
return nil
}
// GenericContextMemory represents a named memory object in the generic context, which can be one of multiple actual
// different memory types. The name for each type within a generic context should be unique.
type GenericContextMemory struct {
Name string `json:"name"`
Type string `json:"type"`
RawValue json.RawMessage `json:"value"`
Block *GenericContextMemoryBlock `json:"-"`
Pointer *GenericContextPointer `json:"-"`
Struct *GenericContextStruct `json:"-"`
Int *GenericContextInt `json:"-"`
}
// MarshalJSON implements json.Marshaler
func (m *GenericContextMemory) MarshalJSON() ([]byte, error) {
if m.Block != nil {
m.Type = "block"
b, err := json.Marshal(m.Block)
if err != nil {
return nil, err
}
m.RawValue = b
}
if m.Pointer != nil {
m.Type = "ptr"
b, err := json.Marshal(m.Pointer)
if err != nil {
return nil, err
}
m.RawValue = b
}
if m.Struct != nil {
m.Type = "struct"
b, err := json.Marshal(m.Struct)
if err != nil {
return nil, err
}
m.RawValue = b
}
if m.Int != nil {
m.Type = "int"
b, err := json.Marshal(m.Int)
if err != nil {
return nil, err
}
m.RawValue = b
}
type Alias GenericContextMemory
a := Alias(*m)
return json.Marshal(a)
}
// UnmarshalJSON implements json.Unmarshaler
func (m *GenericContextMemory) UnmarshalJSON(b []byte) error {
// Creating an alias of the current type makes it so that we can call json.Unmarshal on Alias without
// causing a UnmarshalJSON->UnmarshalJSON infinite look
type Alias GenericContextMemory
var a Alias
err := json.Unmarshal(b, &a)
if err != nil {
return err
}
*m = GenericContextMemory(a)
switch m.Type {
case "block":
m.Block = &GenericContextMemoryBlock{}
err = json.Unmarshal(m.RawValue, m.Block)
case "ptr":
m.Pointer = &GenericContextPointer{}
err = json.Unmarshal(m.RawValue, m.Pointer)
case "struct":
m.Struct = &GenericContextStruct{}
err = json.Unmarshal(m.RawValue, m.Struct)
case "int":
m.Int = &GenericContextInt{}
err = json.Unmarshal(m.RawValue, m.Int)
default:
return fmt.Errorf("'%s' is not a valid context memory type", m.Type)
}
// Delete raw value to make object dumps more clear
m.RawValue = nil
return err
}
// GenericContextMemoryBlock is a block of memory, the `Value` will be loaded into the memory controller as a
// PlainMemory object.
type GenericContextMemoryBlock struct {
Value []byte
ByteOrder binary.ByteOrder
// Address where the struct is loaded in virtual memory
addr uint32
}
// pseudoGenericContextMemoryBlock is a copy of GenericContextMemoryBlock but with string fields which is used while
// marshaling/unmarshalling json.
type pseudoGenericContextMemoryBlock struct {
Value string `json:"value"`
ByteOrder string `json:"byteorder"`
}
// MarshalJSON implements json.Marshaler
func (m *GenericContextMemoryBlock) MarshalJSON() ([]byte, error) {
if m.ByteOrder == nil {
m.ByteOrder = GetNativeEndianness()
}
pseudo := pseudoGenericContextMemoryBlock{
Value: base64.StdEncoding.EncodeToString(m.Value),
ByteOrder: m.ByteOrder.String(),
}
return json.Marshal(pseudo)
}
// UnmarshalJSON implements json.Unmarshaler
func (m *GenericContextMemoryBlock) UnmarshalJSON(b []byte) error {
var pseudo pseudoGenericContextMemoryBlock
err := json.Unmarshal(b, &pseudo)
if err != nil {
return err
}
m.Value, err = base64.StdEncoding.DecodeString(pseudo.Value)
if err != nil {
return err
}
switch strings.ToLower(pseudo.ByteOrder) {
case "le", "littleendian", "little-endian":
m.ByteOrder = binary.LittleEndian
case "be", "bigendian", "big-endian":
m.ByteOrder = binary.BigEndian
default:
return fmt.Errorf("'%s' is not a valid byte order", pseudo.ByteOrder)
}
return nil
}
// GetAddr returns the virtual address of block of memory within the process. If the block is not yet loaded, calling
// this function will cause the load. If the block was already loaded, the existing memory address is returned.
func (m *GenericContextMemoryBlock) GetAddr(p *Process, g *GenericContextMemory) (uint32, error) {
if m.addr != 0 {
return m.addr, nil
}
if m.ByteOrder == nil {
m.ByteOrder = GetNativeEndianness()
}
mem := PlainMemory{
Backing: make([]byte, len(m.Value)),
ByteOrder: m.ByteOrder,
}
copy(mem.Backing, m.Value)
entry, err := p.VM.MemoryController.AddEntry(&mem, uint32(len(m.Value)), g.Name)
if err != nil {
return 0, err
}
m.addr = entry.Addr
return m.addr, nil
}
// Cleanup will remove the memory block from the memory controller of the given process.
func (m *GenericContextMemoryBlock) Cleanup(p *Process) error {
if m.addr == 0 {
return nil
}
err := p.VM.MemoryController.DelEntryByAddr(m.addr)
if err != nil {
return err
}
m.addr = 0
return nil
}
// GenericContextPointer is a pointer to other memory defined in GenericContext.Memory. Offset is the offset from the
// start of the memory block in bytes and Size is the size of the pointer in bits(32 or 64).
// Pointers can only point to "block" and "struct" memory objects.
type GenericContextPointer struct {
Memory string `json:"memory"`
Offset int `json:"offset"`
Size int `json:"size"`
}
// GetValue returns the value of the pointer(the address of the memory we point to plus the allocation)
func (ptr *GenericContextPointer) GetValue(p *Process, g *GenericContext) (uint32, error) {
var mem *GenericContextMemory
for i, m := range g.Memory {
if m.Name == ptr.Memory {
mem = &g.Memory[i]
break
}
}
if mem == nil {
return 0, fmt.Errorf("pointer refers to memory '%s' which doesn't exist", ptr.Memory)
}
switch mem.Type {
case "struct":
addr, err := mem.Struct.GetAddr(p, g, mem)
if err != nil {
return 0, err
}
return addr + uint32(ptr.Offset), nil
case "block":
addr, err := mem.Block.GetAddr(p, mem)
if err != nil {
return 0, err
}
return addr + uint32(ptr.Offset), nil
default:
return 0, fmt.Errorf("can't create pointer to memory of type '%s'", mem.Type)
}
}
// GenericContextStruct is a memory object which describes a memory structure which can be translated into a PlainMemory
// object and loaded into the memory controller of a process.
type GenericContextStruct struct {
Fields []GenericContextStructField
// Address where the struct is loaded in virtual memory
addr uint32
}
// MarshalJSON implements json.Marshaler
func (s *GenericContextStruct) MarshalJSON() ([]byte, error) {
return json.Marshal(s.Fields)
}
// UnmarshalJSON implements json.Unmarshaler
func (s *GenericContextStruct) UnmarshalJSON(b []byte) error {
return json.Unmarshal(b, &s.Fields)
}
// GetAddr returns the virtual address of the structure. The call to this function will cause the struct to be
// registered with the memory controller of the process, subsequent calls will return the same address.
func (s *GenericContextStruct) GetAddr(p *Process, g *GenericContext, m *GenericContextMemory) (uint32, error) {
if s.addr != 0 {
return s.addr, nil
}
var blob []byte
for i, field := range s.Fields {
var mem *GenericContextMemory
for j, m := range g.Memory {
if m.Name == field.Memory {
mem = &g.Memory[j]
break
}
}
if mem == nil {
return 0, fmt.Errorf("field '%s'(%d) refers to memory '%s' which doesn't exist",
field.Name,
i,
field.Memory,
)
}
switch mem.Type {
case "block", "struct":
// TODO including structs in structs isn't uncommon, if we can get the []byte instread of just the addr
// we can just add all bytes to the blob of this struct.
// Note: not planning on allowing blocks directly. That is because of the edge-case: what if we include a
// block in a struct and make a pointer to it? You would have to a: allocate both separately(might be
// confusing if that was not the users intent), b: have the pointer point to the block inside of the
// struct(now we have ordering issues, how does the pointer know the block is also referenced by a struct?
// ). A way to sidestep the issue is to declare 2 types, "block" for pointers and "array" for embeding.
// We just need to implement "array" TODO...
return 0, fmt.Errorf(
"field '%s'(%d) refers to memory of type '%s', which is not supported, only pointers and ints "+
"can be included in structs",
field.Name,
i,
mem.Type,
)
case "ptr":
val, err := mem.Pointer.GetValue(p, g)
if err != nil {
return 0, fmt.Errorf("pointer field '%s'(%d) returned error: %w", field.Name, i, err)
}
b := make([]byte, 4)
GetNativeEndianness().PutUint32(b, val)
blob = append(blob, b...)
case "int":
val, err := mem.Int.GetValue()
if err != nil {
return 0, fmt.Errorf("int field '%s'(%d) returned error: %w", field.Name, i, err)
}
blob = append(blob, val...)
}
}
mem := PlainMemory{
Backing: blob,
ByteOrder: GetNativeEndianness(),
}
entry, err := p.VM.MemoryController.AddEntry(&mem, uint32(len(blob)), m.Name)
if err != nil {
return 0, fmt.Errorf("mem ctl, add entry: %w", err)
}
s.addr = entry.Addr
return s.addr, nil
}
// Cleanup removes the structure from the processes memory controller
func (s *GenericContextStruct) Cleanup(p *Process) error {
if s.addr == 0 {
return nil
}
err := p.VM.MemoryController.DelEntryByAddr(s.addr)
if err != nil {
return err
}
s.addr = 0
return nil
}
// GenericContextStructField describes a field of GenericContextStruct
type GenericContextStructField struct {
Name string `json:"name"`
Memory string `json:"memory"`
}
// GenericContextInt is a memory object describing a interger value of a specific bit size(8, 16, 32, or 64).
type GenericContextInt struct {
Value int64 `json:"value"`
Size int `json:"size"`
}
// GetValue returns the bytes for the given integer in the native byte order.
func (i *GenericContextInt) GetValue() ([]byte, error) {
switch i.Size {
case 8:
return []byte{byte(i.Value)}, nil
case 16:
b := make([]byte, 2)
nativeEndian.PutUint16(b, uint16(i.Value))
return b, nil
case 32:
b := make([]byte, 4)
nativeEndian.PutUint32(b, uint32(i.Value))
return b, nil
case 64:
b := make([]byte, 8)
nativeEndian.PutUint64(b, uint64(i.Value))
return b, nil
default:
return nil, fmt.Errorf("'%d' is an invalid int size", i.Size)
}
}