Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SBOM] Removed prefix in overlayfs mounts #30029

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions pkg/util/containerd/containerd_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"time"

"github.com/hashicorp/go-multierror"
"github.com/opencontainers/image-spec/identity"

"github.com/DataDog/datadog-agent/pkg/config/env"
pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup"
dderrors "github.com/DataDog/datadog-agent/pkg/errors"
"github.com/DataDog/datadog-agent/pkg/util/log"
Expand Down Expand Up @@ -453,15 +454,33 @@ func (c *ContainerdUtil) getMounts(ctx context.Context, expiration time.Duration
return nil, nil, fmt.Errorf("No snapshots returned for image: %s", imageID)
}

// Transforming mounts in case we're running in a container
if env.IsContainerized() {
for i := range mounts {
mounts[i].Source = strings.ReplaceAll(mounts[i].Source, "/var/lib", "/host/var/lib")
for j := range mounts[i].Options {
mounts[i].Options[j] = strings.ReplaceAll(mounts[i].Options[j], "/var/lib", "/host/var/lib")
for i := range mounts {
mounts[i].Source = sanitizePath(mounts[i].Source)

var errs error
for j, opt := range mounts[i].Options {
for _, prefix := range []string{"upperdir=", "lowerdir=", "workdir="} {
if strings.HasPrefix(opt, prefix) {
trimmedOpt := strings.TrimPrefix(opt, prefix)
dirs := strings.Split(trimmedOpt, ":")
for n, dir := range dirs {
dirs[n] = sanitizePath(dir)
if _, err := os.Stat(dirs[n]); err != nil {
errs = multierror.Append(errs, fmt.Errorf("unreachable folder %s for overlayfs mount: %w", dir, err))
}
}
mounts[i].Options[j] = prefix + strings.Join(dirs, ":")
}
}

log.Debugf("Sanitized overlayfs mount options to %s", strings.Join(mounts[i].Options, ","))
}

if errs != nil {
log.Warnf("Unreachable path detected in mounts for image %s: %s", imageID, errs.Error())
}
}

return mounts, func(ctx context.Context) error {
ctx = namespaces.WithNamespace(ctx, namespace)
if err := cleanSnapshot(ctx); err != nil {
Expand All @@ -474,6 +493,14 @@ func (c *ContainerdUtil) getMounts(ctx context.Context, expiration time.Duration
}, nil
}

func sanitizePath(path string) string {
if index := strings.Index(path, "/var/lib"); index != -1 {
return "/host" + path[index:]
}

return path
}

// Mounts returns the mounts for an image
func (c *ContainerdUtil) Mounts(ctx context.Context, expiration time.Duration, namespace string, img containerd.Image) ([]mount.Mount, error) {
mounts, clean, err := c.getMounts(ctx, expiration, namespace, img)
Expand Down
1 change: 1 addition & 0 deletions pkg/util/trivy/trivy.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ func (c *Collector) ScanContainerdImageFromSnapshotter(ctx context.Context, imgM
if err != nil {
return nil, fmt.Errorf("unable to get mounts for image %s, err: %w", imgMeta.ID, err)
}

layers := extractLayersFromOverlayFSMounts(mounts)
if len(layers) == 0 {
return nil, fmt.Errorf("unable to extract layers from overlayfs mounts %+v for image %s", mounts, imgMeta.ID)
Expand Down
Loading