-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphing.go
1067 lines (848 loc) · 21.8 KB
/
graphing.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
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package soaap
import (
"encoding/gob"
"fmt"
"io"
"math"
"os"
"strings"
)
type CallGraph struct {
nodes map[string]GraphNode
roots strset
leaves strset
calls map[Call]int
flows map[Call]int
}
//
// Create a new, empty CallGraph with enough capacity to hold some calls.
//
func NewCallGraph() CallGraph {
return CallGraph{
make(map[string]GraphNode),
make(strset),
make(strset),
make(map[Call]int),
make(map[Call]int),
}
}
//
// Create a CallGraph that contains one of each type of node.
//
func Legend() CallGraph {
cg := NewCallGraph()
vuln := newGraphNode(CallSite{Function: "previously_vulnerable"}, "")
vuln.CVE.Add("CVE-1900-XXXXX")
cg.AddNode(vuln)
mitigated := newGraphNode(
CallSite{Function: "vulnerability_mitigated"}, "sandbox")
mitigated.CVE.Add("CVE-1900-XXXXX")
cg.AddNode(mitigated)
sandboxed := newGraphNode(
CallSite{Function: "sandboxed"}, "sandbox")
cg.AddNode(sandboxed)
private := newGraphNode(CallSite{Function: "private_access"}, "")
private.Owners.Add("priv_owner")
cg.AddNode(private)
ordinary := newGraphNode(CallSite{Function: "ordinary_function"}, "")
cg.AddNode(ordinary)
for _, node := range cg.nodes {
node.Tags.Add("Legend")
}
return cg
}
//
// Load a CallGraph from a binary-encoded file.
//
func LoadGraph(f *os.File, report func(string)) (CallGraph, error) {
var cg CallGraph
dec := gob.NewDecoder(f)
if err := dec.Decode(&cg.nodes); err != nil {
return cg, err
}
if err := dec.Decode(&cg.roots); err != nil {
return cg, err
}
if err := dec.Decode(&cg.leaves); err != nil {
return cg, err
}
if err := dec.Decode(&cg.calls); err != nil {
return cg, err
}
if err := dec.Decode(&cg.flows); err != nil {
return cg, err
}
//
// Reconstitute calls and flows.
//
for call := range cg.calls {
callee := cg.nodes[call.Callee]
callee.CallsIn = append(callee.CallsIn, call)
cg.nodes[call.Callee] = callee
caller := cg.nodes[call.Caller]
caller.CallsOut = append(caller.CallsOut, call)
cg.nodes[call.Caller] = caller
}
for flow := range cg.flows {
dest := cg.nodes[flow.Callee]
dest.FlowsIn = append(dest.FlowsIn, flow)
cg.nodes[flow.Callee] = dest
source := cg.nodes[flow.Caller]
source.FlowsOut = append(source.FlowsOut, flow)
cg.nodes[flow.Caller] = source
}
return cg, nil
}
func (cg *CallGraph) AddCall(call Call) {
cg.calls[call] += 1
caller := call.Caller
callee := call.Callee
// This idiom (copy, modify, update) is terribly tedious, but
// Go maps always return copies rather than references.
c := cg.nodes[caller]
UpdateCalls(&c.CallsOut, call)
cg.nodes[caller] = c
c = cg.nodes[callee]
UpdateCalls(&c.CallsIn, call)
cg.nodes[callee] = c
cg.roots.Remove(callee)
cg.leaves.Remove(caller)
}
func (cg *CallGraph) AddCalls(call Call, weight int) {
cg.AddCall(call)
cg.calls[call] += weight - 1
}
func (cg *CallGraph) AddFlow(flow Call) {
cg.flows[flow] += 1
source := flow.Caller
dest := flow.Callee
c := cg.nodes[source]
UpdateCalls(&c.FlowsOut, flow)
cg.nodes[source] = c
c = cg.nodes[dest]
UpdateCalls(&c.FlowsIn, flow)
cg.nodes[dest] = c
cg.roots.Remove(dest)
cg.leaves.Remove(source)
}
func (cg *CallGraph) AddFlows(flow Call, weight int) {
cg.AddFlow(flow)
cg.flows[flow] += weight - 1
}
func (cg *CallGraph) AddNode(node GraphNode) {
name := node.Name
if n, ok := cg.nodes[name]; ok {
node.Update(n)
}
cg.nodes[name] = node
if len(node.CallsIn) == 0 && len(node.FlowsIn) == 0 {
cg.roots.Add(name)
}
if len(node.CallsOut) == 0 && len(node.FlowsOut) == 0 {
cg.leaves.Add(name)
}
}
//
// Find ancestors of this node by walking both its call graph and its
// data flow graph.
//
// Note that this is different from walking the call-and-data-flow graph:
// it's possible to have calls in one direction and flows in the other,
// yielding cycles in the combined graph.
//
func (cg CallGraph) Ancestors(root string, depth int) strset {
a := cg.CollectNodes(root, GraphNode.Callers, depth)
b := cg.CollectNodes(root, GraphNode.DataSources, depth)
return a.Union(b)
}
func (cg *CallGraph) CollectNodes(root string,
selector func(GraphNode) strset, depth int) strset {
nodes := make(strset)
nodes.Add(root)
if depth == 0 {
return nodes
}
for id := range selector(cg.nodes[root]) {
nodes.Add(id)
children := cg.CollectNodes(id, selector, depth-1)
nodes = nodes.Union(children)
}
return nodes
}
//
// Filter a callgraph to only contain the specified nodes.
//
func (cg CallGraph) Filter(keep strset) CallGraph {
result := NewCallGraph()
for id := range keep {
if node, ok := cg.nodes[id]; ok {
result.AddNode(node)
}
}
for call, weight := range cg.calls {
if keep.Contains(call.Caller) && keep.Contains(call.Callee) {
result.AddCalls(call, weight)
}
}
for flow, weight := range cg.flows {
if keep.Contains(flow.Caller) && keep.Contains(flow.Callee) {
result.AddFlows(flow, weight)
}
}
return result
}
//
// Save a CallGraph to an os.File using a binary encoding.
//
func (cg CallGraph) Save(f *os.File) error {
enc := gob.NewEncoder(f)
//
// We don't want the gob encoder to flatten each node's Call pointers,
// so make a copy of the nodes with no calls.
//
nodes := make(map[string]GraphNode)
for name, node := range cg.nodes {
n := node
n.CallsOut = nil
n.CallsIn = nil
n.FlowsOut = nil
n.FlowsIn = nil
nodes[name] = n
}
if err := enc.Encode(nodes); err != nil {
return err
}
if err := enc.Encode(cg.roots); err != nil {
return err
}
if err := enc.Encode(cg.leaves); err != nil {
return err
}
if err := enc.Encode(cg.calls); err != nil {
return err
}
if err := enc.Encode(cg.flows); err != nil {
return err
}
return nil
}
//
// Simplify a CallGraph by collapsing call chains and dropping any
// unreferenced calls.
//
func (cg CallGraph) Simplified() CallGraph {
g := NewCallGraph()
for r := range cg.roots {
g.addSimplified(cg.nodes[r], cg)
}
return g
}
//
// Recursively add simplified call chains to a CallGraph
//
func (cg *CallGraph) addSimplified(begin GraphNode, old CallGraph) {
cg.AddNode(begin)
for _, f := range begin.FlowsOut {
cg.AddNode(old.nodes[f.Callee])
cg.AddFlow(f)
}
callChain := walkChain(begin, old.nodes)
var next GraphNode
if len(callChain) == 0 {
next = begin
} else {
lastCall := callChain[len(callChain)-1]
next = old.nodes[lastCall.Callee]
cg.AddNode(next)
if len(callChain) == 1 {
cg.AddCall(lastCall)
} else {
call := newCall(begin, next, CallSite{}, lastCall.Sandbox)
weight := 0
for _, call := range callChain {
weight += old.calls[call]
}
cg.AddCalls(call, weight)
}
}
for _, call := range next.CallsOut {
cg.addSimplified(old.nodes[call.Callee], old)
cg.AddCall(call)
}
}
//
// Traverse a linear chain of calls until we encounter an "interesting" node.
//
// Returns the number of calls traversed and the final node in the chain.
//
func walkChain(start GraphNode, nodes map[string]GraphNode) []Call {
chain := make([]Call, 0)
n := start
for {
if !n.IsSimple() || len(n.CallsOut) != 1 {
return chain
}
var call Call
for _, c := range n.CallsOut {
call = c
}
next := nodes[call.Callee]
if len(next.CVE) > 0 || next.Owners.Size() > 0 {
return chain
}
chain = append(chain, call)
n = next
}
return chain
}
//
// Report the size of the graph (number of nodes and number of edges).
//
func (cg CallGraph) Size() (int, int, int) {
return len(cg.nodes), len(cg.calls), len(cg.flows)
}
//
// Add intersecting nodes to this graph, where the call traces leading to
// any two leaf nodes must intersect within `depth` calls.
//
func (cg *CallGraph) AddIntersecting(g CallGraph, depth int) error {
// The method that selects all inputs (callers and data flows)
// into a GraphNode.
selector := GraphNode.AllInputs
// Collect our leaves and their ancestors (up to `depth` calls).
ancestors := make(strset)
for id := range cg.leaves {
ancestors = ancestors.Union(cg.CollectNodes(id, selector, depth))
}
// Keep those leaves with an ancestor common to the above.
keep := make(strset)
for leaf := range g.leaves {
nodes := g.CollectNodes(leaf, selector, depth)
for a := range nodes {
if ancestors.Contains(a) {
keep = keep.Union(nodes)
break
}
}
}
for id := range keep {
cg.AddNode(g.nodes[id])
}
for call, weight := range g.calls {
if keep.Contains(call.Caller) && keep.Contains(call.Callee) {
cg.AddCalls(call, weight)
}
}
for flow, weight := range g.flows {
if keep.Contains(flow.Caller) && keep.Contains(flow.Callee) {
cg.AddFlows(flow, weight)
}
}
return nil
}
//
// Compute the intersection of two CallGraphs, where the call traces leading to
// any two leaf nodes must intersect within `depth` calls.
//
// If `keepBacktrace` is true, in addition to the intersecting nodes, the new
// graph will also contain the full backtrace from each node to its root.
//
func (cg CallGraph) Intersect(g CallGraph, depth int,
keepBacktrace bool) (CallGraph, error) {
// Collect our leaves and their ancestors (up to `depth` calls).
ancestors := make(strset)
for id := range cg.leaves {
ancestors = ancestors.Union(cg.Ancestors(id, depth))
}
// Keep those leaves with an ancestor common to the above.
keep := make(strset)
for leaf := range g.leaves {
nodes := g.Ancestors(leaf, depth)
for a := range nodes {
if ancestors.Contains(a) {
keep = keep.Union(nodes)
if keepBacktrace {
backtrace := g.Ancestors(leaf, -1)
keep = keep.Union(backtrace)
}
break
}
}
}
// Also filter out leaves from the LHS.
ancestors = keep.Clone()
for leaf := range cg.leaves {
nodes := cg.Ancestors(leaf, depth)
for a := range nodes {
if ancestors.Contains(a) {
keep = keep.Union(nodes)
if keepBacktrace {
backtrace := cg.Ancestors(leaf, -1)
keep = keep.Union(backtrace)
}
break
}
}
}
result := cg.Filter(keep)
err := result.Union(g.Filter(keep))
return result, err
}
//
// Compute the union of two CallGraphs.
//
func (cg *CallGraph) Union(g CallGraph) error {
for _, node := range g.nodes {
cg.AddNode(node)
}
for call, count := range g.calls {
cg.AddCalls(call, count)
}
for flow, count := range g.flows {
cg.AddFlows(flow, count)
}
return nil
}
func (cg CallGraph) WriteDot(out io.Writer, groupBy string) error {
rankdir := "BT"
if len(cg.calls) == 0 && len(cg.flows) == 0 {
rankdir = "LR"
}
fmt.Fprintf(out, `digraph {
graph [ fontname = "Inconsolata" ];
node [ fontname = "Inconsolata" ];
edge [ fontname = "Avenir" ];
labeljust = "l";
labelloc = "b";
rankdir = "%s";
`, rankdir)
ungrouped := make([]string, 0)
if groupBy == "" {
for name := range cg.nodes {
ungrouped = append(ungrouped, name)
}
} else {
nodeGroups := make(map[string][]string)
for _, n := range cg.nodes {
var groupName string
switch groupBy {
case "function":
groupName = n.Function
case "library":
groupName = n.Library
case "namespace":
functionName := strings.Split(n.Function, "(")[0]
components := strings.Split(functionName, "::")
if len(components) <= 1 {
groupName = ""
} else {
groupName = strings.Join(components[:len(components)-1], "::")
n.Function = n.Function[len(groupName)+2:]
cg.nodes[n.Name] = n
}
case "sandbox":
groupName = n.Sandbox
case "tags":
groupName = n.Tags.Join(",")
default:
return fmt.Errorf("unknown grouping strategy '%s'", groupBy)
}
if groupName != "" {
nodeGroups[groupName] = append(nodeGroups[groupName], n.Name)
} else {
ungrouped = append(ungrouped, n.Name)
}
}
for name, nodes := range nodeGroups {
fmt.Fprintf(out, " subgraph \"cluster_%s\" {\n", name)
fmt.Fprintf(out, " graph [ bgcolor = \"#dddddd66\" ];\n")
fmt.Fprintf(out, " label = \"%s\";\n", name)
for _, n := range nodes {
fmt.Fprintf(out, " %s\n", cg.nodes[n].Dot())
}
fmt.Fprintf(out, " }\n\n")
}
}
for _, n := range ungrouped {
fmt.Fprintf(out, " %s\n", cg.nodes[n].Dot())
}
for c, count := range cg.calls {
fmt.Fprintf(out, " %s\n", c.Dot(cg, count, false))
}
for c, count := range cg.flows {
fmt.Fprintf(out, " %s\n", c.Dot(cg, count, true))
}
fmt.Fprintf(out, "}\n")
return nil
}
//
// A node in a call graph.
//
// This is derived from a call site or other program location, but can have
// an arbitrary name and description appropriate to a particular analysis.
//
type GraphNode struct {
Name string
// The name of the function this node is in / represents.
Function string
// The library that the function is defined in.
Library string
// The sandbox that this code is being executed in.
//
// Note that SOAAP can discriminate among the same function executing
// in different sandboxes.
Sandbox string
// A vulnerability (current or previous) is known at this location.
CVE strset
// The name of the sandbox(es) that own the data being accessed.
Owners strset
CallsIn []Call
CallsOut []Call
FlowsIn []Call
FlowsOut []Call
Tags strset
}
func newGraphNode(cs CallSite, sandbox string) GraphNode {
var node GraphNode
node.Name = cs.Function + " : " + sandbox
node.Function = cs.Function
node.Library = cs.Location.Library
node.Sandbox = sandbox
node.CVE = make(strset)
node.Owners = make(strset)
node.CallsIn = make([]Call, 0)
node.CallsOut = make([]Call, 0)
node.FlowsIn = make([]Call, 0)
node.FlowsOut = make([]Call, 0)
node.Tags = make(strset)
return node
}
func (n GraphNode) AllInputs() strset {
return n.Callers().Union(n.DataSources())
}
func (n GraphNode) AllOutputs() strset {
return n.Callees().Union(n.DataSinks())
}
func (n GraphNode) Callees() strset {
callees := strset{}
for _, call := range n.CallsOut {
callees.Add(call.Callee)
}
return callees
}
func (n GraphNode) Callers() strset {
callers := strset{}
for _, call := range n.CallsIn {
callers.Add(call.Caller)
}
return callers
}
func (n GraphNode) DataSinks() strset {
sinks := strset{}
for _, flow := range n.FlowsOut {
sinks.Add(flow.Callee)
}
return sinks
}
func (n GraphNode) DataSources() strset {
sources := strset{}
for _, flow := range n.FlowsIn {
sources.Add(flow.Caller)
}
return sources
}
//
// A node is "simple" (or uninteresting, or boring) if none of the
// following "interesting" characteristics apply:
//
// - it has multiple inputs (it joins chains together)
// - it has no inputs (it's a root node)
// - it has multiple outputs (it splits chains apart)
// - it has no outputs (it's a leaf node)
// - it has been marked as previously-vulnerable
// - it accesses private data
//
func (n GraphNode) IsSimple() bool {
return len(n.AllInputs()) == 1 &&
len(n.AllOutputs()) == 1 &&
n.CVE.Size() == 0 &&
n.Owners.Size() == 0
}
//
// Colours that represent different kinds of sandboxes, data, etc.
//
const (
Contained = "#ffff33"
PrivateData = "#3399ff"
Sandboxed = "#66ff66"
Unspecified = "#999999"
Vulnerable = "#ff6666"
)
//
// Construct a GraphViz Dot description of a GraphNode.
//
// This applies SOAAP-specific styling depending on a node's tags.
//
func (n GraphNode) Dot() string {
label := n.Function
// Trim long node names
if len(label) > 30 {
//
// Split node name into <function>(<parameters>) - if applicable.
// Some node names don't have parameters (e.g., those that c++filt
// failed to demangle for some reason).
//
function := strings.Split(n.Function, "(")[0]
if len(function) == len(n.Function) {
// There are no parameters: perhaps demangling failed?
panic(fmt.Errorf("foo: '%s' '%s'", function, n.Function))
}
parameters := n.Function[len(function)+1 : len(n.Function)-1]
plen := 28 - len(function)
if plen < 0 {
plen = 0
} else if plen > len(parameters) {
plen = len(parameters)
}
parameters = parameters[:plen]
label = fmt.Sprintf("%s(%s [...])", function, parameters)
}
if len(n.CVE) > 0 {
label += "\n" + n.CVE.TransformEach("[[%s]]").Join(" ")
}
if n.Sandbox != "" {
label += "\n<<" + n.Sandbox + ">>"
}
colour := Unspecified
switch true {
case len(n.CVE) > 0 && n.Sandbox != "":
colour = Contained
case len(n.CVE) > 0:
colour = Vulnerable
case len(n.Owners) > 0:
colour = PrivateData
case n.Sandbox != "":
colour = Sandboxed
}
colour = colour + "44" // transparency
attrs := map[string]interface{}{
"fillcolor": colour,
"label": label,
"style": "filled",
}
switch true {
case len(n.CVE) > 0 && len(n.Owners) > 0:
attrs["shape"] = "doubleoctagon"
case len(n.Owners) > 0:
attrs["shape"] = "invhouse"
case len(n.CVE) > 0:
attrs["shape"] = "octagon"
}
if n.Sandbox != "" {
attrs["style"] = "dashed,filled"
}
return fmt.Sprintf("\"%s\" %s;", n.Name, dotAttrs(attrs))
}
func (n *GraphNode) Update(g GraphNode) {
if n.Library == "" {
n.Library = g.Library
}
UpdateCalls(&n.CallsIn, g.CallsIn...)
UpdateCalls(&n.CallsOut, g.CallsOut...)
UpdateCalls(&n.FlowsIn, g.FlowsIn...)
UpdateCalls(&n.FlowsOut, g.FlowsOut...)
n.CVE.Union(g.CVE)
n.Owners.Union(g.Owners)
n.Tags.Union(g.Tags)
}
func UpdateCalls(current *[]Call, calls ...Call) {
// This is O(n x m), but n and m are typically very small...
for _, c := range calls {
alreadyHave := false
for _, existing := range *current {
if existing == c {
alreadyHave = true
break
}
}
if !alreadyHave {
*current = append(*current, c)
}
}
}
type Call struct {
// Identifier of the caller.
Caller string
// Identifier of the callee.
Callee string
// Location of the call.
CallSite SourceLocation
// The name of the sandbox the call is occurring in
// (or the empty string if unsandboxed).
Sandbox string
}
func newCall(caller GraphNode, callee GraphNode, cs CallSite, sandbox string) Call {
return Call{
Caller: caller.Name,
Callee: callee.Name,
CallSite: cs.Location,
Sandbox: sandbox,
}
}
// Output GraphViz for a Call.
func (c Call) Dot(graph CallGraph, weight int, flow bool) string {
caller := graph.nodes[c.Caller]
callee := graph.nodes[c.Callee]
label := c.CallSite.String()
style := ""
width := 1 + math.Log(float64(weight))
var colour string
if flow {
colour = PrivateData
style = "dashed"
weight = 0
} else if c.Sandbox == "" {
colour = Unspecified
} else {
colour = Sandboxed
}
attrs := map[string]interface{}{
"color": colour + "cc",
"fontcolor": colour,
"label": label,
"penwidth": width,
"style": style,
"weight": weight,
}
return fmt.Sprintf("\"%s\" -> \"%s\" %s;\n",
caller.Name, callee.Name, dotAttrs(attrs))
}
func (c Call) String() string {
return fmt.Sprintf("'%s' -> '%s'", c.Caller, c.Callee)
}
//
// A function that extracts a CallGraph from SOAAP Results.
//
type graphFn func(results Results, progress func(string)) (CallGraph, error)
var graphExtractors map[string]graphFn = map[string]graphFn{
"privaccess": PrivAccessGraph,
"vuln": VulnGraph,
}
func GraphAnalyses() []string {
keys := make([]string, len(graphExtractors))
i := 0
for k, _ := range graphExtractors {
keys[i] = k
i++
}
return keys
}
type callMaker func(GraphNode, GraphNode, CallSite)
type nodeMaker func(CallSite) GraphNode
//
// Construct a callgraph from SOAAP's vulnerability analysis.
//
func VulnGraph(results Results, progress func(string)) (CallGraph, error) {
graph := NewCallGraph()
for _, v := range results.Vulnerabilities {
trace := results.Traces[v.Trace]
fn := func(cs CallSite) GraphNode {
return newGraphNode(cs, v.Sandbox)
}
call := func(caller GraphNode, callee GraphNode, cs CallSite) {
graph.AddCall(newCall(caller, callee, cs, v.Sandbox))
}
top := fn(v.CallSite)
top.CVE = v.CVEs()
graph.AddNode(top)
g, err := trace.graph(top, results.Traces, fn, call)
if err != nil {
return CallGraph{}, err
}
graph.Union(g)
}
return graph, nil
}
//
// Construct a callgraph of sandbox-private data accesses outside of sandboxes.
//
func PrivAccessGraph(results Results, progress func(string)) (CallGraph, error) {
graph := NewCallGraph()
accesses := results.PrivateAccess
total := len(accesses)
chunk := int(math.Pow(10, math.Ceil(math.Log10(float64(total)/20))))
if chunk < 1000 {
chunk = 1000
}
go progress(fmt.Sprintf("Processing %d private accesses", total))
count := 0
for _, a := range accesses {
trace := results.Traces[a.Trace]
fn := func(cs CallSite) GraphNode {
return newGraphNode(cs, "")
}
call := func(caller GraphNode, callee GraphNode, cs CallSite) {
graph.AddCall(newCall(caller, callee, cs, ""))
}
flow := func(caller GraphNode, callee GraphNode, cs CallSite) {
graph.AddFlow(newCall(caller, callee, cs, ""))
}
top := fn(a.CallSite)
top.Owners = a.DataOwners()
graph.AddNode(top)
g, err := trace.graph(top, results.Traces, fn, call)
if err != nil {