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

Cherry-pick #21936 to 7.x: [Auditbeat] dynamically find librpm.so #22024

Merged
merged 1 commit into from
Oct 21, 2020
Merged
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
63 changes: 47 additions & 16 deletions x-pack/auditbeat/module/system/package/rpm_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import (
"errors"
"fmt"
"runtime"
"strings"
"time"
"unsafe"

"debug/elf"

"github.com/coreos/pkg/dlopen"
)

Expand Down Expand Up @@ -204,29 +207,57 @@ func (lib *librpm) close() error {
return nil
}

func openLibrpm() (*librpm, error) {
var librpmNames = []string{
"librpm.so", // with rpm-devel installed
"librpm.so.9", // Fedora 31/32
"librpm.so.8", // Fedora 29/30
"librpm.so.3", // CentOS 7
"librpm.so.1", // CentOS 6

// Following for completeness, but not explicitly tested
"librpm.so.10",
"librpm.so.7",
"librpm.so.6",
"librpm.so.5",
"librpm.so.4",
"librpm.so.2",
// getLibrpmNames determines the versions of librpm.so that are
// installed on a system. rpm-devel rpm installs the librpm.so
// symbolic link to the correct version of librpm, but that isn't a
// required package. rpm will install librpm.so.X, where X is the
// version number. getLibrpmNames looks at the elf header for the rpm
// binary to determine what version of librpm.so it is linked against.
func getLibrpmNames() []string {
var rpmPaths = []string{
"/usr/bin/rpm",
"/bin/rpm",
}
var libNames = []string{
"librpm.so",
}
var rpmElf *elf.File
var err error

for _, path := range rpmPaths {
rpmElf, err = elf.Open(path)
if err == nil {
break
}
}
if err != nil {
return libNames
}

impLibs, err := rpmElf.ImportedLibraries()
if err != nil {
return libNames
}

for _, lib := range impLibs {
if strings.Contains(lib, "librpm.so") {
libNames = append(libNames, lib)
}
}

return libNames
}

func openLibrpm() (*librpm, error) {

var librpm librpm
var err error

librpmNames := getLibrpmNames()

librpm.handle, err = dlopen.GetHandle(librpmNames)
if err != nil {
return nil, err
return nil, fmt.Errorf("Couldn't open %v", librpmNames)
}

librpm.rpmtsCreate, err = librpm.handle.GetSymbolPointer("rpmtsCreate")
Expand Down