-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathanalyze.go
More file actions
482 lines (410 loc) · 13 KB
/
analyze.go
File metadata and controls
482 lines (410 loc) · 13 KB
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
package diff
import (
"bytes"
"fmt"
"math"
"strings"
"github.com/golang-collections/go-datastructures/augmentedtree"
"github.com/google/uuid"
fianoUEFI "github.com/linuxboot/fiano/pkg/uefi"
"github.com/steakknife/hamming"
"github.com/9elements/converged-security-suite/v2/pkg/bootflow/systemartifacts/biosimage"
"github.com/9elements/converged-security-suite/v2/pkg/bootflow/types"
"github.com/9elements/converged-security-suite/v2/pkg/mathtools"
"github.com/9elements/converged-security-suite/v2/pkg/uefi/ffs"
pkgbytes "github.com/linuxboot/fiano/pkg/bytes"
)
const (
// rangeAmountThresholdReduceRanges defines the threshold when it is
// required to try to merge ranges to reduce the calculation time.
rangeAmountThresholdReduceRanges = 1000
// reduceRangesDistance defines the maximal distance between ranges
// to merge them.
reduceRangesDistance = 1023
)
func hammingDistance(a, b []byte, excludeCharsA, excludeCharsB []byte) uint64 {
l := mathtools.Min(len(a), len(b))
a = a[:l]
b = b[:l]
if len(excludeCharsA) == 0 && len(excludeCharsB) == 0 {
return uint64(hamming.Bytes(a, b))
}
var distance uint64
for idx := range a {
if bytes.IndexByte(excludeCharsA, a[idx]) != -1 || bytes.IndexByte(excludeCharsB, b[idx]) != -1 {
continue
}
distance += uint64(hamming.Byte(a[idx], b[idx]))
}
return distance
}
// NodeInfo is a struct contains information about one UEFI FFS node.
type NodeInfo struct {
UUID uuid.UUID
Description string
}
var emptyUUID uuid.UUID
// String implements fmt.Stringer
func (nodeInfo NodeInfo) String() string {
if nodeInfo.Description == "" && bytes.Equal(nodeInfo.UUID[:], emptyUUID[:]) {
return "unknown"
}
if nodeInfo.Description != "" {
return nodeInfo.Description
}
return nodeInfo.UUID.String()
}
// NodeInfos is a slice of NodeInfo-s
type NodeInfos []NodeInfo
// String implements fmt.Stringer
func (s NodeInfos) String() string {
var result []string
for _, node := range s {
result = append(result, node.String())
}
return strings.Join(result, ", ")
}
// RelatedMeasurement contains the related measurement and the data chunks
// specifically related to the diff.
type RelatedMeasurement struct {
RelatedDataChunks DataChunks
Measurement
}
// RelatedMeasurementsLaconic is a helper to print measurements in a laconic way
type RelatedMeasurementsLaconic []RelatedMeasurement
// String implements fmt.Stringer
//
//nolint:typecheck
func (s RelatedMeasurementsLaconic) String() string {
var ids []string
for _, measurement := range s {
chunksComment := laconicChunksString(measurement.RelatedDataChunks)
if chunksComment == "" {
ids = append(ids, measurement.Description)
continue
}
ids = append(ids, measurement.Description+":"+chunksComment)
}
return strings.Join(ids, ", ")
}
func laconicChunksString(chunks DataChunks) string {
var r []string
for _, chunk := range chunks {
if chunk.Description != "" {
r = append(r, chunk.Description)
continue
}
if chunk.ForceBytes != nil {
r = append(r, fmt.Sprintf("ForceBytes:%X", chunk.ForceBytes))
continue
}
r = append(r, fmt.Sprintf("Ref:%v", chunk.Reference))
}
return strings.Join(r, ",")
}
// AnalysisReportEntry contains information about on block with different data.
type AnalysisReportEntry struct {
// DiffRange is the information about offsets where the data is different.
DiffRange pkgbytes.Range
// HammingDistance is a bit-wise hamming distance between the data blocks.
HammingDistance uint64
// HammingDistanceNon00orFF is a bit-wise hamming distance between the data
// blocks, excluding bytes 0x00 and 0xff
HammingDistanceNon00orFF uint64
// RelatedMeasurements contains the list of measurements which overlaps
// with the data block.
RelatedMeasurements []RelatedMeasurement
// Nodes contains the list of UEFI nodes (regions, volumes, modules, files)
// which overlaps with the data block
Nodes []NodeInfo
}
// AnalysisReportEntries is a set of multiple AnalysisReportEntry-ies.
type AnalysisReportEntries []AnalysisReportEntry
// DiffRanges returns DiffRange-s.
func (s AnalysisReportEntries) DiffRanges() pkgbytes.Ranges {
result := make(pkgbytes.Ranges, 0, len(s))
for _, entry := range s {
result = append(result, entry.DiffRange)
}
return result
}
// AnalysisReport contains an analyzed report for an UEFI image diff.
type AnalysisReport struct {
// Entries contains each block with different data.
Entries AnalysisReportEntries
// AddressMapper converts the address in DiffRange into a position in the SystemArtifact.
AddressMapper types.AddressMapper
// FirstProblemOffset is the offset of the first byte with a different value.
FirstProblemOffset uint64
// BytesChanged is a count of bytes with different values.
BytesChanged uint64
// HammingDistance is a bit-wise hamming distance between images.
HammingDistance uint64
// HammingDistanceNon00orFF is a bit-wise hamming distance between images, excluding
// bytes 0x00 and 0xff
HammingDistanceNon00orFF uint64
}
// Firmware is an abstraction over *uefi.UEFI
type Firmware interface {
Buf() []byte
GetByRange(byteRange pkgbytes.Range) (nodes []*ffs.Node, err error)
NameToRangesMap() map[string]pkgbytes.Ranges
}
type DataChunk struct {
Description string
ForceBytes []byte
Reference pkgbytes.Range
AddressMapper types.AddressMapper
CustomData any
}
type DataChunks []DataChunk
type Measurement struct {
Description string
Chunks DataChunks
CustomData any
}
type Measurements []Measurement
// Analyze generates a difference report filled with additional simple
// analytics, like hamming distance.
func Analyze(
diffMemRanges Ranges,
memMapper types.AddressMapper,
measurements Measurements,
goodFirmware, badFirmware *biosimage.BIOSImage,
) (report AnalysisReport, err error) {
diffMemRanges.Sort()
diffMemRanges = pkgbytes.MergeRanges(diffMemRanges, 0)
if len(diffMemRanges) > rangeAmountThresholdReduceRanges {
diffMemRanges = pkgbytes.MergeRanges(diffMemRanges, reduceRangesDistance)
}
rangesGood, err := memMapper.Resolve(goodFirmware, diffMemRanges...)
if err != nil {
return AnalysisReport{}, fmt.Errorf("unable to resolve the good image ranges: %w", err)
}
if len(diffMemRanges) != len(rangesGood) {
return AnalysisReport{}, fmt.Errorf("currently Analyze only one-to-one range mapping, but %d != %d", len(diffMemRanges), len(rangesGood))
}
rangesBad, err := memMapper.Resolve(badFirmware, diffMemRanges...)
if err != nil {
return AnalysisReport{}, fmt.Errorf("unable to resolve the bad image ranges: %w", err)
}
if len(rangesGood) != len(rangesBad) {
return AnalysisReport{}, fmt.Errorf("currently Analyze only supports the case where the amount of ranges for both images are the same, but %d != %d", len(rangesGood), len(rangesBad))
}
goodData := goodFirmware.Content
goodUEFI, err := goodFirmware.Parse()
if err != nil {
return AnalysisReport{}, fmt.Errorf("unable to parse the UEFI layout: %w", err)
}
badData := badFirmware.Content
// Preparing data structures to quickly find UEFI nodes overlapping with
// a byte range.
allNodes, err := goodUEFI.GetByRange(pkgbytes.Range{
Offset: 0,
Length: uint64(len(goodData)),
})
if err != nil {
return AnalysisReport{}, fmt.Errorf("unable to scan for UEFI nodes: %w", err)
}
nodesIntervalTree := newNodesIntervalTree(allNodes)
namesIntervalTree := newNamesIntervalTree(goodUEFI.NameToRangesMap())
// Preparing a report
report.AddressMapper = memMapper
report.FirstProblemOffset = math.MaxUint64
for idx, rM := range diffMemRanges {
rG := rangesGood[idx]
rB := rangesBad[idx]
entryEndOffsetG := rG.Offset + rG.Length
entryEndOffsetB := rB.Offset + rB.Length
entryDataG := goodData[rG.Offset:entryEndOffsetG]
entryDataB := badData[rB.Offset:entryEndOffsetB]
var relatedMeasurements []RelatedMeasurement
for _, m := range measurements {
var relatedDataChunks DataChunks
for _, data := range m.Chunks {
if data.Reference.Intersect(rM) {
relatedDataChunks = append(relatedDataChunks, data)
}
}
if len(relatedDataChunks) == 0 {
continue
}
relatedMeasurements = append(relatedMeasurements, RelatedMeasurement{
RelatedDataChunks: relatedDataChunks,
Measurement: m,
})
}
// Filling some analysisEntry fields
analysisEntry := AnalysisReportEntry{
DiffRange: rM,
HammingDistance: hammingDistance(entryDataG, entryDataB, nil, nil),
HammingDistanceNon00orFF: hammingDistance(entryDataG, entryDataB, nil, []byte{0x00, 0xff}),
RelatedMeasurements: relatedMeasurements,
}
// Filling analysisEntry.Nodes
//
// analysisEntry.Nodes should contain a list of UEFI nodes (regions,
// volumes, modules, files) which overlaps with the diffRange.
var overlappedNodes []*ffs.Node
for _, node := range nodesIntervalTree.FindOverlapping(rM) {
overlappedNodes = append(overlappedNodes, node.(*ffs.Node))
}
if len(overlappedNodes) == 0 {
// We use an ugly `unsafe` hack to extract bytes ranges,
// because https://github.com/linuxboot/fiano/pull/317 is still
// not fixed (and therefore not merged).
//
// And a bytes range is not always detected, so sometimes
// nodesIntervalTree.FindOverlapping(diffRange) will return zero entries
// even if the range is definitely related to some nodes. In this
// case we use a fallback way, which is more reliable, but returns
// only names (instead of *ffs.Node objects).
for _, name := range namesIntervalTree.FindOverlapping(rM) {
analysisEntry.Nodes = append(analysisEntry.Nodes, NodeInfo{
Description: name.(string),
})
}
} else {
analysisEntry.Nodes = GetNodesInfo(overlappedNodes)
}
// Filling report
if rM.Offset < report.FirstProblemOffset {
report.FirstProblemOffset = rM.Offset
}
report.BytesChanged += rM.Length
report.HammingDistance += analysisEntry.HammingDistance
report.HammingDistanceNon00orFF += analysisEntry.HammingDistanceNon00orFF
report.Entries = append(report.Entries, analysisEntry)
}
return
}
type intervalTree struct {
augmentedtree.Tree
}
type interval struct {
IDValue uint64
pkgbytes.Range
Value interface{}
}
func (item *interval) LowAtDimension(_ uint64) int64 {
return int64(item.Offset)
}
func (item *interval) HighAtDimension(_ uint64) int64 {
return int64(item.Offset + item.Length)
}
func (item *interval) OverlapsAtDimension(cmpIface augmentedtree.Interval, _ uint64) bool {
cmp := cmpIface.(*interval)
return item.Intersect(cmp.Range)
}
func (item *interval) ID() uint64 {
return item.IDValue
}
func newNodesIntervalTree(nodes []*ffs.Node) intervalTree {
t := intervalTree{
Tree: augmentedtree.New(1),
}
for idx, node := range nodes {
t.Add(&interval{
IDValue: uint64(idx),
Range: node.Range,
Value: node,
})
}
return t
}
func newNamesIntervalTree(m map[string]pkgbytes.Ranges) intervalTree {
t := intervalTree{
Tree: augmentedtree.New(1),
}
idx := uint64(0)
for name, ranges := range m {
for _, _range := range ranges {
t.Add(&interval{
IDValue: idx,
Range: _range,
Value: name,
})
idx++
}
}
return t
}
func (t *intervalTree) FindOverlapping(r pkgbytes.Range) []interface{} {
var result []interface{}
for _, item := range t.Query(&interval{
Range: r,
}) {
result = append(result, item.(*interval).Value)
}
return result
}
// AddOffset just adds the offset to all offsets of the report
//
//nolint:typecheck
func (report *AnalysisReport) AddOffset(offset int64) {
if report == nil {
return
}
report.FirstProblemOffset += uint64(offset)
for idx := range report.Entries {
entry := &report.Entries[idx]
entry.DiffRange.Offset += uint64(offset)
for idx := range entry.RelatedMeasurements {
measurement := entry.RelatedMeasurements[idx]
for idx := range measurement.RelatedDataChunks {
measurement.RelatedDataChunks[idx].Reference.Offset += uint64(offset)
}
for idx := range measurement.Chunks {
measurement.Chunks[idx].Reference.Offset += uint64(offset)
}
}
}
}
// GetNodesInfo converts nodes to structures ready for human-readable printing.
//
// TODO: move this to a "format" package
func GetNodesInfo(nodes []*ffs.Node) NodeInfos {
var result NodeInfos
for _, node := range nodes {
// Gathering information
var nodeType string
var id string
moduleName := node.ModuleName()
switch f := node.Firmware.(type) {
case *fianoUEFI.FirmwareVolume:
nodeType = "volume"
case *fianoUEFI.File:
nodeType = "file"
case *fianoUEFI.BIOSRegion:
nodeType = "bios_region"
default:
nodeType = fmt.Sprintf("%T", f)
if stringer, ok := f.(fmt.Stringer); ok {
id = stringer.String()
}
}
if id == `` {
guid := node.GUID()
if guid != nil {
id = guid.String()
}
}
// Compiling the string for the node
var description strings.Builder
description.WriteString(nodeType)
if id != "" {
description.WriteString(":" + id)
}
if moduleName != nil {
description.WriteString(":" + *moduleName)
}
// Appending
// TODO: do not String()-ify and then parse UUID, just use it as is.
uuidValue, _ := uuid.Parse(id)
result = append(result, NodeInfo{
UUID: uuidValue,
Description: description.String(),
})
}
return result
}