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

x-pack/auditbeat/module/system/process: don't try to hash files in other namespaces #29786

Merged
merged 1 commit into from
Jan 19, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d

*Auditbeat*

- system/process: Prevent hashing files in other mnt namespaces. {issue}25777[25777] {issue}29678[29678] {pull}29786[29786]

*Filebeat*

Expand Down
57 changes: 57 additions & 0 deletions x-pack/auditbeat/module/system/process/namepace_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

//go:build linux
// +build linux

package process

import (
"fmt"
"os"
"syscall"

"github.com/pkg/errors"
)

// isNsSharedWith returns whether the process with the given pid shares the
// namespace ns with the current process.
func isNsSharedWith(pid int, ns string) (yes bool, err error) {
self, err := selfNsIno(ns)
if err != nil {
return false, err
}
other, err := nsIno(pid, ns)
if err != nil {
return false, err
}
return self == other, nil
}

// selfNsIno returns the inode number for the namespace ns for this process.
func selfNsIno(ns string) (ino uint64, err error) {
fi, err := os.Stat(fmt.Sprintf("/proc/self/ns/%s", ns))
if err != nil {
return 0, err
}
sysInfo, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
return 0, errors.New("not a stat_t")
}
return sysInfo.Ino, nil
}

// nsIno returns the inode number for the namespace ns for the process with
// the given pid.
func nsIno(pid int, ns string) (ino uint64, err error) {
fi, err := os.Stat(fmt.Sprintf("/proc/%d/ns/%s", pid, ns))
if err != nil {
return 0, err
}
sysInfo, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
return 0, errors.New("not a stat_t")
}
return sysInfo.Ino, nil
}
13 changes: 13 additions & 0 deletions x-pack/auditbeat/module/system/process/namepace_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

//go:build !linux
// +build !linux

package process

// isNsSharedWith returns true and nil.
func isNsSharedWith(pid int, ns string) (yes bool, err error) {
return true, nil
}
15 changes: 13 additions & 2 deletions x-pack/auditbeat/module/system/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,26 @@ func (ms *MetricSet) enrichProcess(process *Process) {
}

if process.Info.Exe != "" {
sharedMntNS, err := isNsSharedWith(process.Info.PID, "mnt")
if err != nil {
if process.Error == nil {
process.Error = errors.Wrapf(err, "failed to get namespaces for %v PID %v", process.Info.Exe,
process.Info.PID)
}
return
}
if !sharedMntNS {
return
}
hashes, err := ms.hasher.HashFile(process.Info.Exe)
if err != nil {
if process.Error == nil {
process.Error = errors.Wrapf(err, "failed to hash executable %v for PID %v", process.Info.Exe,
process.Info.PID)
}
} else {
process.Hashes = hashes
return
}
process.Hashes = hashes
}
}

Expand Down