From 1440742a1ce375dbc3da9fb67680328bbf06b73e Mon Sep 17 00:00:00 2001 From: Maisem Ali Date: Tue, 1 Nov 2022 16:15:16 +0500 Subject: [PATCH] ssh/tailssh: use root / as cmd.Dir when users HomeDir doesn't exist Fixes #5224 Signed-off-by: Maisem Ali --- ssh/tailssh/incubator.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ssh/tailssh/incubator.go b/ssh/tailssh/incubator.go index 86f7b835d..f0c12436c 100644 --- a/ssh/tailssh/incubator.go +++ b/ssh/tailssh/incubator.go @@ -283,7 +283,16 @@ func (ss *sshSession) launchProcess() error { ss.cmd = ss.newIncubatorCommand() cmd := ss.cmd - cmd.Dir = ss.conn.localUser.HomeDir + homeDir := ss.conn.localUser.HomeDir + if _, err := os.Stat(homeDir); err == nil { + cmd.Dir = homeDir + } else if os.IsNotExist(err) { + // If the home directory doesn't exist, we can't chdir to it. + // Instead, we'll chdir to the root directory. + cmd.Dir = "/" + } else { + return err + } cmd.Env = append(cmd.Env, envForUser(ss.conn.localUser)...) for _, kv := range ss.Environ() { if acceptEnvPair(kv) {