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

Fallback to root when user's home directory is not accessible #47524

Merged
merged 1 commit into from
Oct 24, 2024

Conversation

eriktate
Copy link
Contributor

@eriktate eriktate commented Oct 11, 2024

This PR adds an additional check to CheckHomeDir to see if the login user has access to their configured home directory. If not, we fall back to logging them in under the root directory which is the current behavior when their home directory doesn't exist.

changelog: Fixed an issue preventing connections with users whose configured home directories were inaccessible.

lib/srv/reexec.go Outdated Show resolved Hide resolved
lib/srv/reexec.go Outdated Show resolved Hide resolved
lib/srv/reexec.go Outdated Show resolved Hide resolved
func CheckHomeDir(localUser *user.User) (bool, error) {
if fi, err := os.Stat(localUser.HomeDir); err == nil {
return fi.IsDir(), nil
err := HasAccessibleHomeDir(localUser)
Copy link
Contributor

Choose a reason for hiding this comment

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

Funnily enough, simple os.Stat() is likely more complete check due to leveraging the actual system stack to do the check.

What is the problem with the root user?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What is the problem with the root user?

Can you add to this? I'm not sure I understand what you're asking 🤔

if trace.IsAccessDenied(err) {
return false, nil
// don't spawn a subcommand if already running as the user in question
if currentUser.Uid == localUser.Uid {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems like there was an assumption baked in that CheckHomeDir would always be called by the root/primary user, but I didn't observe that during testing. In fact I never actually saw the subcommand getting spawned while testing ssh connections through the web UI 🤔 This check prevents dropping into another sub process if we're already running as the target user

@eriktate eriktate force-pushed the tross/home_dir_perms branch from 3ec066b to a3e2b84 Compare October 16, 2024 18:10
homeDir = "/"
hasAccess, err := CheckHomeDirAccess(localUser)
if err != nil {
return errorWriter, teleport.RemoteCommandFailure, trace.Wrap(err, "failed to confirm access to home directory")
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to abort here? Should this be surfaced as an error and the command proceed using / instead of the home directory?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I think that probably makes the most sense. I'm now writing the error directly to errorWriter and then carrying on. If there's a better way to do that, I'm definitely open to it since I'm not entirely sure this output would ever land anywhere 🤔 I'm also not sure it matters since interactive sessions ultimately end up with a message sent back to the client that says something to the effect of Could not set shell's cwd to home directory "/home/ubuntu", defaulting to "/". We could maybe add some additional context to that message explaining why (e.g. directory doesn't exist, no access, or something unexpected)

lib/srv/reexec.go Outdated Show resolved Hide resolved
lib/srv/reexec.go Outdated Show resolved Hide resolved
@eriktate eriktate force-pushed the tross/home_dir_perms branch 3 times, most recently from 1d6706d to 7b4037b Compare October 17, 2024 19:19
lib/srv/exec_linux_test.go Outdated Show resolved Hide resolved
lib/srv/exec_linux_test.go Outdated Show resolved Hide resolved
lib/srv/reexec.go Outdated Show resolved Hide resolved
Comment on lines 1269 to 1276
if trace.IsNotFound(err) || trace.IsAccessDenied(err) || trace.IsBadParameter(err) {
return false, err
}

return false, trace.Wrap(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any reason to return an unwrapped error in some cases? Can the inner if be removed?

Suggested change
if trace.IsNotFound(err) || trace.IsAccessDenied(err) || trace.IsBadParameter(err) {
return false, err
}
return false, trace.Wrap(err)
return false, trace.Wrap(err)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No I think I accidentally pushed some changes I was testing with. Line 1270 should actually be return false, nil.

lib/srv/reexec.go Outdated Show resolved Hide resolved
lib/srv/reexec.go Outdated Show resolved Hide resolved
lib/srv/reexec.go Outdated Show resolved Hide resolved
Copy link
Contributor

@Tener Tener left a comment

Choose a reason for hiding this comment

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

Looks good aside for usage of os.PathSeparator.

lib/srv/reexec.go Outdated Show resolved Hide resolved
lib/srv/reexec.go Outdated Show resolved Hide resolved
@eriktate eriktate force-pushed the tross/home_dir_perms branch from bad6ae8 to cec15fa Compare October 23, 2024 19:29
@public-teleport-github-review-bot public-teleport-github-review-bot bot removed the request for review from tigrato October 24, 2024 06:40
access and child processes fallback to the root directory ("/") in the
case that they do not
@eriktate eriktate force-pushed the tross/home_dir_perms branch from cec15fa to aae1b74 Compare October 24, 2024 15:21
@eriktate eriktate enabled auto-merge October 24, 2024 15:21
@eriktate eriktate added this pull request to the merge queue Oct 24, 2024
Merged via the queue into master with commit 05d3371 Oct 24, 2024
39 checks passed
@eriktate eriktate deleted the tross/home_dir_perms branch October 24, 2024 19:35
@public-teleport-github-review-bot

@eriktate See the table below for backport results.

Branch Result
branch/v14 Failed
branch/v15 Failed
branch/v16 Failed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants