-
Notifications
You must be signed in to change notification settings - Fork 2
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
feat(lib): use memfd on linux instead of dumping libddwaf.so in /tmp #106
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -40,22 +40,22 @@ type wafSymbols struct { | |||||
run uintptr | ||||||
} | ||||||
|
||||||
// newWafDl loads the libddwaf shared library and resolves all tge relevant symbols. | ||||||
// NewWafDl loads the libddwaf shared library and resolves all tge relevant symbols. | ||||||
// The caller is responsible for calling wafDl.Close on the returned object once they | ||||||
// are done with it so that associated resources can be released. | ||||||
func NewWafDl() (dl *WafDl, err error) { | ||||||
file, err := lib.DumpEmbeddedWAF() | ||||||
file, closer, err := lib.DumpEmbeddedWAF() | ||||||
if err != nil { | ||||||
return | ||||||
} | ||||||
defer func() { | ||||||
if rmErr := os.Remove(file); rmErr != nil { | ||||||
err = errors.Join(err, fmt.Errorf("error removing %s: %w", file, rmErr)) | ||||||
if rmErr := closer(); rmErr != nil { | ||||||
err = errors.Join(err, fmt.Errorf("error removing %s: %w", file.Name(), rmErr)) | ||||||
} | ||||||
}() | ||||||
|
||||||
var handle uintptr | ||||||
if handle, err = purego.Dlopen(file, purego.RTLD_GLOBAL|purego.RTLD_NOW); err != nil { | ||||||
if handle, err = purego.Dlopen(file.Name(), purego.RTLD_GLOBAL|purego.RTLD_NOW); err != nil { | ||||||
return | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
|
@@ -97,12 +97,12 @@ func (waf *WafDl) Close() error { | |||||
return purego.Dlclose(waf.handle) | ||||||
} | ||||||
|
||||||
// wafGetVersion returned string is a static string so we do not need to free it | ||||||
// WafGetVersion returned string is a static string so we do not need to free it | ||||||
func (waf *WafDl) WafGetVersion() string { | ||||||
return unsafe.Gostring(unsafe.Cast[byte](waf.syscall(waf.getVersion))) | ||||||
} | ||||||
|
||||||
// wafInit initializes a new WAF with the provided ruleset, configuration and info objects. A | ||||||
// WafInit initializes a new WAF with the provided ruleset, configuration and info objects. A | ||||||
// cgoRefPool ensures that the provided input values are not moved or garbage collected by the Go | ||||||
// runtime during the WAF call. | ||||||
func (waf *WafDl) WafInit(ruleset *WafObject, config *WafConfig, info *WafObject) WafHandle { | ||||||
|
@@ -125,7 +125,7 @@ func (waf *WafDl) WafDestroy(handle WafHandle) { | |||||
unsafe.KeepAlive(handle) | ||||||
} | ||||||
|
||||||
// wafKnownAddresses returns static strings so we do not need to free them | ||||||
// WafKnownAddresses returns static strings so we do not need to free them | ||||||
func (waf *WafDl) WafKnownAddresses(handle WafHandle) []string { | ||||||
var nbAddresses uint32 | ||||||
|
||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-present Datadog, Inc. | ||
|
||
//go:build darwin && (amd64 || arm64) && !go1.24 && !datadog.no_waf && (cgo || appsec) | ||
|
||
package lib | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
_ "embed" | ||
) | ||
|
||
//go:embed .version | ||
var EmbeddedWAFVersion string | ||
|
||
func DumpEmbeddedWAF() (file *os.File, closer func() error, err error) { | ||
file, err = os.CreateTemp("", embedNamePattern) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("error creating temp file: %w", err) | ||
} | ||
|
||
defer func() { | ||
if err != nil { | ||
if closeErr := file.Close(); closeErr != nil { | ||
err = errors.Join(err, fmt.Errorf("error closing file: %w", closeErr)) | ||
} | ||
if rmErr := os.Remove(file.Name()); rmErr != nil { | ||
err = errors.Join(err, fmt.Errorf("error removing file: %w", rmErr)) | ||
} | ||
} | ||
}() | ||
|
||
gr, err := gzip.NewReader(bytes.NewReader(libddwaf)) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("error creating gzip reader: %w", err) | ||
} | ||
|
||
if _, err := io.Copy(file, gr); err != nil { | ||
eliottness marked this conversation as resolved.
Show resolved
Hide resolved
eliottness marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil, nil, fmt.Errorf("error copying gzip content to file: %w", err) | ||
} | ||
|
||
if err := gr.Close(); err != nil { | ||
return nil, nil, fmt.Errorf("error closing gzip reader: %w", err) | ||
} | ||
|
||
return file, func() error { | ||
return errors.Join(file.Close(), os.Remove(file.Name())) | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-present Datadog, Inc. | ||
|
||
//go:build linux && (amd64 || arm64) && !go1.24 && !datadog.no_waf && (cgo || appsec) | ||
|
||
package lib | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"errors" | ||
"fmt" | ||
"golang.org/x/sys/unix" | ||
"io" | ||
"os" | ||
|
||
_ "embed" | ||
) | ||
|
||
//go:embed .version | ||
var EmbeddedWAFVersion string | ||
eliottness marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
func DumpEmbeddedWAF() (file *os.File, closer func() error, err error) { | ||
|
||
eliottness marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fd, err := unix.MemfdCreate("libddwaf", 0) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("error creating memfd: %w", err) | ||
} | ||
|
||
file = os.NewFile(uintptr(fd), fmt.Sprintf("/proc/self/fd/%d", fd)) | ||
if file == nil { | ||
return nil, nil, errors.New("error creating file from fd") | ||
} | ||
|
||
defer func() { | ||
if file != nil && err != nil { | ||
if closeErr := file.Close(); closeErr != nil { | ||
err = errors.Join(err, fmt.Errorf("error closing file: %w", closeErr)) | ||
} | ||
} | ||
}() | ||
|
||
gr, err := gzip.NewReader(bytes.NewReader(libddwaf)) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("error creating gzip reader: %w", err) | ||
} | ||
|
||
if _, err := io.Copy(file, gr); err != nil { | ||
eliottness marked this conversation as resolved.
Show resolved
Hide resolved
eliottness marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil, nil, fmt.Errorf("error copying gzip content to memfd: %w", err) | ||
} | ||
|
||
if err := gr.Close(); err != nil { | ||
return nil, nil, fmt.Errorf("error closing gzip reader: %w", err) | ||
} | ||
|
||
return file, file.Close, nil | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we not ignore this
err
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah sh*t. Should have disabled auto-merge when I saw your comments. Don't worry I will do it in another PR