-
Notifications
You must be signed in to change notification settings - Fork 5k
[Heartbeat] Add prctl dumpable flag reset after cap drop #38269
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
Conversation
Pinging @elastic/obs-ds-hosted-services (Team:obs-ds-hosted-services) |
This pull request does not have a backport label.
To fixup this pull request, you need to add the backport labels for the needed
|
prctl(2) says:
Is it really dropping capabilities that is clearing the dumpable flag? This program doesn't agree, but maybe I'm missing something... package main
import (
"fmt"
"log"
"golang.org/x/sys/unix"
"kernel.org/pub/linux/libs/security/libcap/cap"
)
func main() {
if true {
newcaps := cap.GetProc()
// Raise all permitted caps to effective
if err := newcaps.Fill(cap.Effective, cap.Permitted); err != nil {
log.Fatal(err)
}
// Drop all inheritable caps to stop propagation to child proc
if err := newcaps.ClearFlag(cap.Inheritable); err != nil {
log.Fatal(err)
}
// Apply the new capabilities to the current process (incl. all threads)
if err := newcaps.SetProc(); err != nil {
log.Fatal(err)
}
}
dumpable, err := cap.Prctl(unix.PR_GET_DUMPABLE)
if err != nil {
log.Fatal(err)
}
fmt.Println("dumpable:", dumpable)
select {}
}
|
Hi @axw, I think your tests is missing file capabilities, like the ones we specify for heartbeat bundled inside the agent. Same code with file caps: $ go build && chmod +x go_playground
$ sudo setcap cap_net_raw,cap_setuid+p go_playground
$ ./go_playground
dumpable: 0 It's not the act of dropping capabilities that triggers the change, but the mechanism we have in place for locking down capabilities. I apologize if the reference induced any confusion. |
No worries! I understand what's going on now, thanks :) I missed that we were changing capabilities on the heartbeat binary. |
@@ -46,6 +47,9 @@ func init() { | |||
// The beat should use `getcap` at a later point to examine available capabilities | |||
// rather than relying on errors from `setcap` | |||
_ = setCapabilities() | |||
|
|||
// Make heartbeat dumpable so elastic-agent can access process metrics. |
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.
Question: Is this applicable only when
- We are not running as root, common in context of container (BEAT_SETUID_AS) and under elastic-agent.
- We effectively switched the user id and group id which changed the dumpable flag to value other than 1 (restircting access).
With that said, Should we enforce this change only on containerized environment? I assume there is no harm in overriding the dumpable flag even if its already 1?
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.
I assume there is no harm in overriding the dumpable flag even if its already 1?
There shouldn't be, there's no issue with overriding and in case of error, kernel returns an error code we can safely ignore since the agent is ready to handle those cases now.
We are not running as root, common in context of container (BEAT_SETUID_AS) and under elastic-agent.
We effectively switched the user id and group id which changed the dumpable flag to value other than 1 (restircting access).
Not exactly, elastic-agent image is already shipped with file capabilities enabled so it doesn't require BEAT_SETUID_AS
mechanism to kick in, see this point in prctl:
• The process executes ([execve(2)](https://man7.org/linux/man-pages/man2/execve.2.html)) a program that has
file capabilities (see [capabilities(7)](https://man7.org/linux/man-pages/man7/capabilities.7.html)), but only if
the permitted capabilities gained exceed those already
permitted for the process.
Although changing effective uid/gid also triggers disabling dumpable flag.
Should we enforce this change only on containerized environment?
Container detection can be unreliable in some cases. Also, I can't think of a situation where this would broaden the permission scope for a non-containerized instance (running users already have access to config files). But I'm happy to discuss alternatives.
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.
Gotcha, Thanks for the extra context. I agree with the argument here on container detection and permission scope. I think the workaround looks good as it is.
💚 Build Succeeded
History
cc @emilioalvap |
💚 Build Succeeded
History
cc @emilioalvap |
💚 Build Succeeded
cc @emilioalvap |
💔 Build Failed
Failed CI Stepscc @emilioalvap |
💚 Build Succeeded
History
cc @emilioalvap |
💔 Build Failed
Failed CI Stepscc @emilioalvap |
💚 Build Succeeded
History
cc @emilioalvap |
💔 Build Failed
Failed CI Stepscc @emilioalvap |
💔 Build Failed
Failed CI Steps
History
cc @emilioalvap |
💔 Build Failed
Failed CI StepsHistory
cc @emilioalvap |
Enforce dumpable attribute on heartbeat process for /proc/io to be readable by elastic-agent. --------- Co-authored-by: Vignesh Shanmugam <[email protected]> (cherry picked from commit 9c9ae35)
Enforce dumpable attribute on heartbeat process for /proc/io to be readable by elastic-agent. --------- Co-authored-by: Vignesh Shanmugam <[email protected]> (cherry picked from commit 9c9ae35)
…8369) Enforce dumpable attribute on heartbeat process for /proc/io to be readable by elastic-agent. --------- Co-authored-by: Vignesh Shanmugam <[email protected]> (cherry picked from commit 9c9ae35) Co-authored-by: Emilio Alvarez Piñeiro <[email protected]>
…8368) Enforce dumpable attribute on heartbeat process for /proc/io to be readable by elastic-agent. --------- Co-authored-by: Vignesh Shanmugam <[email protected]> (cherry picked from commit 9c9ae35) Co-authored-by: Emilio Alvarez Piñeiro <[email protected]>
Proposed commit message
Heartbeat lockdown measures are triggering
/proc/io
ownership rules, which end up not being accessible by the agent. This PR introduces a reset onPRCTL_SET_DUMPABLE
flag to allow the agent to access heartbeat metrics.Checklist
[ ] I have added tests that prove my fix is effective or that my feature worksCHANGELOG.next.asciidoc
orCHANGELOG-developer.next.asciidoc
.How to test this PR locally
docker exec -u root -it --container name-- ps -aef
/proc/--pid--/io
: