github.com/google/osv-scalibr@v0.4.1/annotator/osduplicate/dpkg/dpkg.go (about)

     1  // Copyright 2025 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package dpkg implements an annotator for language packages that have already been found in
    16  // DPKG OS packages.
    17  package dpkg
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"fmt"
    23  	"io"
    24  	"strings"
    25  
    26  	"github.com/google/osv-scalibr/annotator"
    27  	"github.com/google/osv-scalibr/annotator/osduplicate"
    28  	"github.com/google/osv-scalibr/common/linux/dpkg"
    29  	"github.com/google/osv-scalibr/inventory"
    30  	"github.com/google/osv-scalibr/inventory/vex"
    31  	"github.com/google/osv-scalibr/plugin"
    32  )
    33  
    34  const (
    35  	// Name of the Annotator.
    36  	Name = "vex/os-duplicate/dpkg"
    37  )
    38  
    39  // Annotator adds annotations to language packages that have already been found in DPKG OS packages.
    40  type Annotator struct{}
    41  
    42  // New returns a new Annotator.
    43  func New() annotator.Annotator { return &Annotator{} }
    44  
    45  // Name of the annotator.
    46  func (Annotator) Name() string { return Name }
    47  
    48  // Version of the annotator.
    49  func (Annotator) Version() int { return 0 }
    50  
    51  // Requirements of the annotator.
    52  func (Annotator) Requirements() *plugin.Capabilities {
    53  	return &plugin.Capabilities{OS: plugin.OSLinux}
    54  }
    55  
    56  // Annotate adds annotations to language packages that have already been found in DPKG OS packages.
    57  func (a *Annotator) Annotate(ctx context.Context, input *annotator.ScanInput, results *inventory.Inventory) error {
    58  	locationToPKGs := osduplicate.BuildLocationToPKGsMap(results, input.ScanRoot)
    59  
    60  	it, err := dpkg.NewListFilePathIterator(input.ScanRoot.FS)
    61  	if err != nil {
    62  		return fmt.Errorf("failed to create dpkg file iterator: %w", err)
    63  	}
    64  	defer it.Close()
    65  
    66  	errs := []error{}
    67  	for {
    68  		// Return if canceled or exceeding deadline.
    69  		if err := ctx.Err(); err != nil {
    70  			errs = append(errs, fmt.Errorf("%s halted at %q because of context error: %w", a.Name(), input.ScanRoot.Path, err))
    71  			break
    72  		}
    73  
    74  		filePath, err := it.Next(ctx)
    75  		if err != nil {
    76  			if !errors.Is(err, io.EOF) {
    77  				errs = append(errs, err)
    78  			}
    79  			break
    80  		}
    81  
    82  		// Remove leading '/' since SCALIBR fs paths don't include that.
    83  		filePath = strings.TrimPrefix(filePath, "/")
    84  		if pkgs, ok := locationToPKGs[filePath]; ok {
    85  			for _, pkg := range pkgs {
    86  				pkg.ExploitabilitySignals = append(pkg.ExploitabilitySignals, &vex.PackageExploitabilitySignal{
    87  					Plugin: Name,
    88  					// TODO(b/425890695): This exclusion doesn't quite match the use case here: The component
    89  					// is present but already tracked by another Extractor (os/dpkg). We should consider
    90  					// introducing a new type to better describe these cases.
    91  					Justification:   vex.ComponentNotPresent,
    92  					MatchesAllVulns: true,
    93  				})
    94  			}
    95  		}
    96  	}
    97  
    98  	return errors.Join(errs...)
    99  }