-
Notifications
You must be signed in to change notification settings - Fork 155
/
parser_csaf.go
327 lines (286 loc) · 10.5 KB
/
parser_csaf.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
//
// Copyright 2023 The GUAC Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package csaf
import (
"context"
"fmt"
"strconv"
jsoniter "github.com/json-iterator/go"
"github.com/guacsec/guac/pkg/assembler"
"github.com/guacsec/guac/pkg/assembler/clients/generated"
"github.com/guacsec/guac/pkg/assembler/helpers"
"github.com/guacsec/guac/pkg/handler/processor"
"github.com/guacsec/guac/pkg/ingestor/parser/common"
"github.com/guacsec/guac/pkg/logging"
"github.com/openvex/go-vex/pkg/csaf"
)
var (
json = jsoniter.ConfigCompatibleWithStandardLibrary
justificationsMap = map[string]generated.VexJustification{
"component_not_present": generated.VexJustificationComponentNotPresent,
"vulnerable_code_not_present": generated.VexJustificationVulnerableCodeNotPresent,
"vulnerable_code_not_in_execute_path": generated.VexJustificationVulnerableCodeNotInExecutePath,
"vulnerable_code_cannot_be_controlled_by_adversary": generated.VexJustificationVulnerableCodeCannotBeControlledByAdversary,
"inline_mitigations_already_exist": generated.VexJustificationInlineMitigationsAlreadyExist,
}
vexStatusMap = map[string]generated.VexStatus{
"known_not_affected": generated.VexStatusNotAffected,
"known_affected": generated.VexStatusAffected,
"fixed": generated.VexStatusFixed,
"first_fixed": generated.VexStatusFixed,
"under_investigation": generated.VexStatusUnderInvestigation,
"first_affected": generated.VexStatusAffected,
"last_affected": generated.VexStatusAffected,
"recommended": generated.VexStatusAffected,
}
)
type csafParser struct {
doc *processor.Document
identifierStrings *common.IdentifierStrings
csaf *csaf.CSAF
}
type visitedProductRef struct {
productName string
productID string
name string
category string
}
func NewCsafParser() common.DocumentParser {
return &csafParser{
identifierStrings: &common.IdentifierStrings{},
}
}
// Parse breaks out the document into the graph components
func (c *csafParser) Parse(ctx context.Context, doc *processor.Document) error {
c.doc = doc
err := json.Unmarshal(doc.Blob, &c.csaf)
if err != nil {
return fmt.Errorf("failed to parse CSAF: %w", err)
}
return nil
}
// GetIdentities gets the identity node from the document if they exist
func (c *csafParser) GetIdentities(ctx context.Context) []common.TrustInformation {
return nil
}
func (c *csafParser) GetIdentifiers(ctx context.Context) (*common.IdentifierStrings, error) {
return c.identifierStrings, nil
}
// findPurl searches the given CSAF product tree recursively to find the
// purl for the specified product reference.
//
// It recursively calls itself on each child branch of the tree in a
// depth first search manner if the nodes name isn't equal to product_ref.
func findPurl(ctx context.Context, tree csaf.ProductBranch, product_ref string) *string {
return findPurlSearch(ctx, tree, product_ref, make(map[string]bool))
}
func findPurlSearch(ctx context.Context, tree csaf.ProductBranch, product_ref string, visited map[string]bool) *string {
if tree.Name == product_ref {
purl := tree.Product.IdentificationHelper["purl"]
return &purl
}
for i, b := range tree.Branches {
key := fmt.Sprintf("%v-%v-%v", tree.Name, strconv.Itoa(i), b.Name)
if visited[key] {
continue
}
visited[key] = true
purl := findPurlSearch(ctx, b, product_ref, visited)
if purl != nil {
return purl
}
}
return nil
}
// findProductRef searches for a product reference string for the given product ID
// by recursively traversing the CSAF product tree.
//
// findProductRefSearch was seperated from findProductRef so that the code can use
// a visited map and avoid infinite recursion.
//
// It returns a pointer to the product reference string if found,
// otherwise nil.
func findProductRef(ctx context.Context, tree csaf.ProductBranch, product_id string) *string {
return findProductRefSearch(ctx, tree, product_id, make(map[visitedProductRef]bool))
}
// findProductRefSearch recursively searches the product tree for a product
// reference matching the given product ID. It does this with a visited map to
// avoid infinite recursion.
//
// It returns a pointer to the product reference string if found,
// otherwise nil.
func findProductRefSearch(ctx context.Context, tree csaf.ProductBranch, product_id string, visited map[visitedProductRef]bool) *string {
if visited[visitedProductRef{tree.Product.Name, tree.Product.ID, tree.Name, tree.Category}] {
return nil
}
visited[visitedProductRef{tree.Product.Name, tree.Product.ID, tree.Name, tree.Category}] = true
for _, r := range tree.Relationships {
if r.FullProductName.ID == product_id {
return &r.ProductRef
}
}
for _, b := range tree.Branches {
pref := findProductRefSearch(ctx, b, product_id, visited)
if pref != nil {
return pref
}
}
return nil
}
// findActionStatement searches the given Vulnerability tree to find the action statement
// for the product with the given product_id. If a matching product is found, it returns
// a pointer to the Details string for the Remediation.
// If no matching product is found, it returns nil.
func findActionStatement(tree *csaf.Vulnerability, product_id string) *string {
for _, r := range tree.Remediations {
for _, p := range r.ProductIDs {
if p == product_id {
return &r.Details
}
}
}
return nil
}
// findImpactStatement searches the given Vulnerability tree to find the impact statement
// for the product with the given product_id. If a matching product is found, it returns
// a pointer to the Details string for the Threat.
// If no matching product is found, it returns nil.
func findImpactStatement(tree *csaf.Vulnerability, product_id string) *string {
for _, t := range tree.Threats {
if t.Category == "impact" {
for _, p := range t.ProductIDs {
if p == product_id {
return &t.Details
}
}
}
}
return nil
}
// findPkgSpec finds the package specification for the product with the
// given ID in the CSAF document. It returns a pointer to the package
// specification if found, otherwise an error.
func (c *csafParser) findPkgSpec(ctx context.Context, product_id string) (*generated.PkgInputSpec, error) {
pref := findProductRef(ctx, c.csaf.ProductTree, product_id)
if pref == nil {
return nil, fmt.Errorf("unable to locate product reference for id %s", product_id)
}
purl := findPurl(ctx, c.csaf.ProductTree, *pref)
if purl == nil {
return nil, fmt.Errorf("unable to locate product url for reference %s", *pref)
}
return helpers.PurlToPkg(*purl)
}
// generateVexIngest generates a VEX ingest object from a CSAF vulnerability and other input data.
//
// The function maps CSAF data into a VEX ingest object for adding to the vulnerability graph.
// It then tries to find the package for the product ID to add to ingest.
// If it can't be found, it then returns nil, otherwise it returns a pointer to the VEX ingest object.
func (c *csafParser) generateVexIngest(ctx context.Context, vulnInput *generated.VulnerabilityInputSpec, csafVuln *csaf.Vulnerability, status string, product_id string) *assembler.VexIngest {
logger := logging.FromContext(ctx)
vi := &assembler.VexIngest{}
vd := generated.VexStatementInputSpec{}
vd.KnownSince = c.csaf.Document.Tracking.CurrentReleaseDate
vd.Origin = c.csaf.Document.Tracking.ID
vd.VexJustification = generated.VexJustificationNotProvided
if vexStatus, ok := vexStatusMap[status]; ok {
vd.Status = vexStatus
}
var statement *string
if vd.Status == generated.VexStatusNotAffected {
statement = findImpactStatement(csafVuln, product_id)
} else {
statement = findActionStatement(csafVuln, product_id)
}
if statement != nil {
vd.Statement = *statement
}
for _, flag := range csafVuln.Flags {
found := false
for _, pid := range flag.ProductIDs {
if pid == product_id {
found = true
}
}
if found {
if just, ok := justificationsMap[flag.Label]; ok {
vd.VexJustification = just
}
}
}
vi.VexData = &vd
vi.Vulnerability = vulnInput
c.identifierStrings.PurlStrings = append(c.identifierStrings.PurlStrings, product_id)
pkg, err := c.findPkgSpec(ctx, product_id)
if err != nil {
logger.Warnf("[csaf] unable to locate package for not-affected product %s", product_id)
return nil
}
vi.Pkg = pkg
return vi
}
// GetPredicates generates the VEX and CertifyVuln predicates for the CSAF document.
//
// It returns a pointer to an assembler.IngestPredicates struct containing the
// generated VEX and CertifyVuln predicates.
func (c *csafParser) GetPredicates(ctx context.Context) *assembler.IngestPredicates {
rv := &assembler.IngestPredicates{}
var vis []assembler.VexIngest
var cvs []assembler.CertifyVulnIngest
for _, v := range c.csaf.Vulnerabilities {
vuln, err := helpers.CreateVulnInput(v.CVE)
if err != nil {
return nil
}
statuses := []string{"fixed", "known_not_affected", "known_affected", "first_affected", "first_fixed", "last_affected", "recommended", "under_investigation"}
for _, status := range statuses {
products := v.ProductStatus[status]
for _, product := range products {
vi := c.generateVexIngest(ctx, vuln, &v, status, product)
if vi == nil {
continue
}
if status == "known_affected" || status == "under_investigation" {
vulnData := generated.ScanMetadataInput{
TimeScanned: c.csaf.Document.Tracking.CurrentReleaseDate,
}
cv := assembler.CertifyVulnIngest{
Pkg: vi.Pkg,
Vulnerability: vuln,
VulnData: &vulnData,
}
cvs = append(cvs, cv)
} else if status == "known_not_affected" || status == "fixed" {
vulnData := generated.ScanMetadataInput{
TimeScanned: c.csaf.Document.Tracking.CurrentReleaseDate,
}
noVuln := generated.VulnerabilityInputSpec{
Type: "NoVuln",
}
cv := assembler.CertifyVulnIngest{
Pkg: vi.Pkg,
Vulnerability: &noVuln,
VulnData: &vulnData,
}
cvs = append(cvs, cv)
}
vis = append(vis, *vi)
}
}
}
rv.Vex = vis
rv.CertifyVuln = cvs
return rv
}