From ff3bb5883054c4709d14f9fa8ffbb5fbd2f5b1b3 Mon Sep 17 00:00:00 2001 From: James Tucker Date: Sat, 24 Oct 2026 00:25:23 +0000 Subject: [PATCH] ssh/tailssh: validate accepted env values, deny GLIBC_TUNABLES, cap names GLIBC_TUNABLES is parsed by glibc in root su/login environments with AT_SECURE=0 (CVE-2023-4911 class); add it to the dangerous-env-var denylist next to its GCONV_PATH/LOCPATH siblings. The TERM/LANG/LC_* pairs accepted without policy (OpenSSH's default AcceptEnv) had unvalidated values that reach root-exec'd login/su environments, where glibc treats path-like locale names as locale-data paths and leading dashes as its own options. Reject path- and option-like values (and NUL, which would truncate on exec) instead of trying to sanitize them. Also cap accepted env var names at 256 bytes before any wildcard matching, bounding the matcher input and the child's "su -w" allowlist; real names are short (LOGIN_NAME_MAX). Updates tailscale/corp#50380 Signed-off-by: James Tucker --- ssh/tailssh/accept_env.go | 26 +++++++++++++++++++++----- ssh/tailssh/accept_env_test.go | 11 +++++++++++ ssh/tailssh/incubator.go | 19 +++++++++++++++++-- ssh/tailssh/tailssh_test.go | 17 +++++++++++++++++ 4 files changed, 66 insertions(+), 7 deletions(-) diff --git a/ssh/tailssh/accept_env.go b/ssh/tailssh/accept_env.go index 4e2edf099..fe66612dc 100644 --- a/ssh/tailssh/accept_env.go +++ b/ssh/tailssh/accept_env.go @@ -17,15 +17,18 @@ import ( // even when a wildcard acceptEnv pattern like "*" is configured. // // The denylist also covers other root su/login environment exposure -// classes: glibc module/file lookups (GCONV_PATH, LOCPATH, NLSPATH) that -// can load attacker-controlled files into privileged processes, and -// resolver configuration (HOSTALIASES, RES_OPTIONS) that can redirect -// privileged hostname lookups. +// classes: glibc module/file lookups and tunables parsing +// (GCONV_PATH, LOCPATH, NLSPATH, GLIBC_TUNABLES; CVE-2023-4911 class — +// glibc parses tunables in root su/login environments with +// AT_SECURE=0) that can load attacker-controlled files or code into +// privileged processes, and resolver configuration (HOSTALIASES, +// RES_OPTIONS) that can redirect privileged hostname lookups. func isDangerousEnvVar(name string) bool { upper := strings.ToUpper(name) return strings.HasPrefix(upper, "LD_") || strings.HasPrefix(upper, "DYLD_") || upper == "GOTRACEBACK" || upper == "GCONV_PATH" || upper == "LOCPATH" || - upper == "NLSPATH" || upper == "HOSTALIASES" || upper == "RES_OPTIONS" + upper == "NLSPATH" || upper == "HOSTALIASES" || upper == "RES_OPTIONS" || + upper == "GLIBC_TUNABLES" } // forbiddenEnvKey reports whether name must never be accepted from the client as a @@ -42,6 +45,12 @@ func forbiddenEnvKey(name string) bool { return name == "" } +// maxAcceptEnvNameLen caps the length of an accepted environment variable +// name. Names are short (LOGIN_NAME_MAX is 256 on Linux); a longer "name" +// is bogus and would only serve to tie up the pattern matcher or bloat the +// child's "su -w" allowlist. +const maxAcceptEnvNameLen = 256 + // filterEnv filters a passed in environ string slice (a slice with strings // representing environment variables in the form "key=value") based on // the supplied slice of acceptEnv values. @@ -69,6 +78,13 @@ func filterEnv(acceptEnv []string, environ []string) ([]string, error) { return nil, fmt.Errorf(`invalid environment variable: %q. Variables must be in "KEY=VALUE" format`, envPair) } + // Bound the matcher and allowlist input: names are short + // (LOGIN_NAME_MAX), so a longer one is bogus. Reject before any + // wildcard matching runs. + if len(variableName) > maxAcceptEnvNameLen { + continue + } + // Reject NUL bytes: envp entries are NUL-terminated, so a NUL would silently truncate on exec if strings.Contains(envPair, "\x00") { continue diff --git a/ssh/tailssh/accept_env_test.go b/ssh/tailssh/accept_env_test.go index 93ba1a7cf..df4c22384 100644 --- a/ssh/tailssh/accept_env_test.go +++ b/ssh/tailssh/accept_env_test.go @@ -39,6 +39,8 @@ func TestIsDangerousEnvVar(t *testing.T) { {"hostaliases", true}, {"RES_OPTIONS", true}, {"res_options", true}, + {"GLIBC_TUNABLES", true}, + {"glibc_tunables", true}, {"TERM", false}, {"LANG", false}, {"LC_ALL", false}, @@ -276,6 +278,15 @@ func TestFilterEnv(t *testing.T) { environ: []string{"MY VAR=1", "WEIRD\tNAME=2", "MY.VAR=3", "MY-VAR=ok", "B\xc3\xa4R=5", "MY_VAR=ok2"}, expectedFiltered: []string{"MY-VAR=ok", "MY_VAR=ok2"}, }, + { + // Names are capped at 256 bytes, even when the pattern would + // match: real names are short, longer ones only tie up the + // matcher or bloat the child's "su -w" allowlist. + name: "oversized-name-rejected", + acceptEnv: []string{"*"}, + environ: []string{strings.Repeat("A", maxAcceptEnvNameLen) + "=x", strings.Repeat("B", maxAcceptEnvNameLen+1) + "=x", "GOOD=1"}, + expectedFiltered: []string{strings.Repeat("A", maxAcceptEnvNameLen) + "=x", "GOOD=1"}, + }, } for _, tc := range testCases { diff --git a/ssh/tailssh/incubator.go b/ssh/tailssh/incubator.go index 685034a7d..6251955ed 100644 --- a/ssh/tailssh/incubator.go +++ b/ssh/tailssh/incubator.go @@ -1167,12 +1167,27 @@ func updateStringInSlice(ss []string, a, b string) { // acceptEnvPair reports whether the environment variable key=value pair // should be accepted from the client. It uses the same default as OpenSSH // AcceptEnv. +// +// The value reaches root-exec'd login/su environments, where glibc treats +// a LANG/LC_* value containing "/" as a path to locale data and a leading +// "-" as its own options; TERM is a terminal capability name, not a path. +// Reject path- and option-like values (and NUL, which would truncate on +// exec) rather than trying to sanitize them. func acceptEnvPair(kv string) bool { - k, _, ok := strings.Cut(kv, "=") + k, v, ok := strings.Cut(kv, "=") if !ok || isDangerousEnvVar(k) || forbiddenEnvKey(k) { return false } - return k == "TERM" || k == "LANG" || strings.HasPrefix(k, "LC_") + if strings.Contains(v, "\x00") { + return false + } + if k == "TERM" { + return !strings.Contains(v, "/") + } + if k == "LANG" || strings.HasPrefix(k, "LC_") { + return !strings.Contains(v, "/") && !strings.HasPrefix(v, "-") + } + return false } func fileExists(path string) bool { diff --git a/ssh/tailssh/tailssh_test.go b/ssh/tailssh/tailssh_test.go index bb4343fca..45066bb1e 100644 --- a/ssh/tailssh/tailssh_test.go +++ b/ssh/tailssh/tailssh_test.go @@ -1284,6 +1284,23 @@ func TestAcceptEnvPair(t *testing.T) { {"LC_FOO=x", true}, {"LD_PRELOAD=naah", false}, {"TERM=screen-256color", true}, + {"TERM=xterm-256color", true}, + // TERM is a capability name, not a path. + {"TERM=a/b", false}, + // Locale names reach root su/login environments where glibc treats + // path-like values as locale-data paths and leading dashes as its + // own options. + {"LANG=en_US.UTF-8", true}, + {"LANG=../..", false}, + {"LANG=../../x", false}, + {"LANG=/x", false}, + {"LANG=-x", false}, + {"LC_FOO=-x", false}, + {"LC_FOO=en_US", true}, + {"LC_FOO=a/b", false}, + // NUL would truncate on exec. + {"TERM=x\x00y", false}, + {"LANG=x\x00y", false}, } for _, tt := range tests { if got := acceptEnvPair(tt.in); got != tt.want {