Skip to content

Commit

Permalink
fix(pkg,internal): multiple fixes related to cos and driver build.
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP authored and poiana committed Jan 23, 2024
1 parent c89a6a8 commit 58b66f5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
20 changes: 17 additions & 3 deletions internal/utils/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func ExtractTarGz(gzipStream io.Reader, destDir string, stripPathComponents int)
switch header.Typeflag {
case tar.TypeDir:
d := filepath.Join(destDir, strippedName)
if err = os.Mkdir(filepath.Clean(d), 0o750); err != nil {
if err = os.MkdirAll(filepath.Clean(d), 0o750); err != nil {
return nil, err
}
files = append(files, d)
Expand All @@ -76,8 +76,22 @@ func ExtractTarGz(gzipStream io.Reader, destDir string, stripPathComponents int)
if err = outFile.Close(); err != nil {
return nil, err
}
if err = os.Chmod(filepath.Clean(f), header.FileInfo().Mode()); err != nil {
return nil, err
}
files = append(files, f)

case tar.TypeLink, tar.TypeSymlink:
strippedSrcName := stripComponents(header.Linkname, stripPathComponents)
fDst := filepath.Join(destDir, strippedName)
if header.Typeflag == tar.TypeSymlink {
err = os.Symlink(filepath.Clean(strippedSrcName), filepath.Clean(fDst))
} else {
err = os.Link(filepath.Clean(strippedSrcName), filepath.Clean(fDst))
}
if err != nil {
return nil, err
}
files = append(files, fDst)
default:
return nil, fmt.Errorf("extractTarGz: uknown type: %b in %s", header.Typeflag, header.Name)
}
Expand All @@ -96,5 +110,5 @@ func stripComponents(headerName string, stripComponents int) string {
if len(names) < stripComponents {
return headerName
}
return filepath.Clean(strings.Join(names[stripComponents:], "/"))
return filepath.Clean(strings.Join(names[stripComponents:], string(os.PathSeparator)))
}
5 changes: 3 additions & 2 deletions pkg/driver/distro/cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package driverdistro
import (
"fmt"
"os"
"path/filepath"

"github.com/blang/semver"
"github.com/falcosecurity/driverkit/pkg/kernelrelease"
Expand Down Expand Up @@ -76,15 +77,15 @@ func (c *cos) customizeBuild(ctx context.Context,

currKernelDir := env[kernelDirEnv]

cosKernelDir := currKernelDir + "usr/src/"
cosKernelDir := filepath.Join(currKernelDir, "usr", "src")
entries, err := os.ReadDir(cosKernelDir)
if err != nil {
return nil, err
}
if len(entries) == 0 {
return nil, fmt.Errorf("no COS kernel src found")
}
cosKernelDir = entries[0].Name()
cosKernelDir = filepath.Join(cosKernelDir, entries[0].Name())
// Override env key
env[kernelDirEnv] = cosKernelDir

Expand Down
11 changes: 8 additions & 3 deletions pkg/driver/distro/distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package driverdistro

import (
"archive/tar"
"compress/gzip"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -336,12 +336,17 @@ func downloadKernelSrc(ctx context.Context,
if err != nil {
return nil, err
}
var src io.Reader
var src io.ReadCloser
if strings.HasSuffix(kernelConfigPath, ".gz") {
src = tar.NewReader(f)
src, err = gzip.NewReader(f)
if err != nil {
return env, err
}
} else {
src = f
}
defer src.Close()

fStat, err := f.Stat()
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions pkg/driver/type/bpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (b *bpf) Build(ctx context.Context,
makeCmdArgs := fmt.Sprintf(`make -C %q`, filepath.Clean(srcPath))
makeCmd := exec.CommandContext(ctx, "bash", "-c", makeCmdArgs) //nolint:gosec // false positive
// Append requested env variables to the command env
makeCmd.Env = os.Environ()
for key, val := range env {
makeCmd.Env = append(makeCmd.Env, fmt.Sprintf("%s=%s", key, val))
}
Expand Down

0 comments on commit 58b66f5

Please sign in to comment.