Skip to content

fix(agent): filter out GOTRACEBACK=none #16924

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

Merged
merged 2 commits into from
Mar 17, 2025
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
5 changes: 4 additions & 1 deletion agent/agentexec/cli_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"golang.org/x/sys/unix"
"golang.org/x/xerrors"
"kernel.org/pub/linux/libs/security/libcap/cap"

"github.com/coder/coder/v2/agent/usershell"
)

// CLI runs the agent-exec command. It should only be called by the cli package.
Expand Down Expand Up @@ -114,7 +116,8 @@ func CLI() error {

// Remove environment variables specific to the agentexec command. This is
// especially important for environments that are attempting to develop Coder in Coder.
env := os.Environ()
ei := usershell.SystemEnvInfo{}
env := ei.Environ()
env = slices.DeleteFunc(env, func(e string) bool {
return strings.HasPrefix(e, EnvProcPrioMgmt) ||
strings.HasPrefix(e, EnvProcOOMScore) ||
Expand Down
12 changes: 11 additions & 1 deletion agent/usershell/usershell.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,17 @@ func (SystemEnvInfo) User() (*user.User, error) {
}

func (SystemEnvInfo) Environ() []string {
return os.Environ()
var env []string
for _, e := range os.Environ() {
// Ignore GOTRACEBACK=none, as it disables stack traces, it can
// be set on the agent due to changes in capabilities.
// https://pkg.go.dev/runtime#hdr-Security.
if e == "GOTRACEBACK=none" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way for us to override this for ourselves that doesn't also override it for any user of coder? Seems like we're choosing for any user of coder that GOTRACEBACK shouldn't be set.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Linux, we could theoretically read /proc/self/environ to figure out if it was set externally or by the Go runtime. I'm not sure it's worth the lift though.

If someone sets this env in, say, a docker container resource. Yes, it will be filtered out for child-processes (which arguably may be the right thing to do anyway). But if someone sets it on the agent resource (which is the recommended way anyway), they absolutely still can.

continue
}
env = append(env, e)
}
return env
}

func (SystemEnvInfo) HomeDir() (string, error) {
Expand Down
9 changes: 9 additions & 0 deletions agent/usershell/usershell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,13 @@ func TestGet(t *testing.T) {
require.NotEmpty(t, shell)
})
})

t.Run("Remove GOTRACEBACK=none", func(t *testing.T) {
t.Setenv("GOTRACEBACK", "none")
ei := usershell.SystemEnvInfo{}
env := ei.Environ()
for _, e := range env {
require.NotEqual(t, "GOTRACEBACK=none", e)
}
})
}
Loading