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

ssh/tailssh: fall back to using su when no TTY available on Linux #11910

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

oxtoacart
Copy link
Contributor

@oxtoacart oxtoacart commented Apr 29, 2024

This allows pam authentication to run, triggering automation
like pam_mkhomedir.

Updates #11854

@@ -3,6 +3,10 @@ FROM ${BASE}

RUN groupadd -g 10000 groupone
RUN groupadd -g 10001 grouptwo
RUN useradd -g 10000 -G 10001 -u 10002 -m testuser
RUN useradd -g 10000 -G 10001 -u 10002 testuser
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removing the -m flag keeps this from creating a home directory for the user (we want PAM to do that on login).

@@ -126,22 +127,7 @@ func (ss *sshSession) newIncubatorCommand() (cmd *exec.Cmd) {
if isShell {
incubatorArgs = append(incubatorArgs, "--shell")
}
// Only the macOS version of the login command supports executing a
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's no need to do this outside of the actual incubator process, so this has moved into tryLoginCmd.

@oxtoacart oxtoacart force-pushed the ox/11854-3 branch 2 times, most recently from 2655cc4 to a1ad9e5 Compare May 1, 2024 12:18
Base automatically changed from ox/11854-2 to main May 1, 2024 16:19
@oxtoacart oxtoacart requested review from maisem and bradfitz May 1, 2024 16:20
@oxtoacart oxtoacart changed the title ssh/tailssh: fall back to su when login is unavailable ssh/tailssh: fall back to using su when no TTY available on Linux May 1, 2024
@oxtoacart oxtoacart force-pushed the ox/11854-3 branch 5 times, most recently from f9f1be8 to 7c3424b Compare May 2, 2024 19:28
Copy link
Contributor

@awly awly left a comment

Choose a reason for hiding this comment

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

This links to https://github.com/tailscale/corp/issues/11854, which is probably the wrong issue.
It would be great to see a description of the problem this PR solves for context (sorry if I missed it somewhere!)

ssh/tailssh/incubator.go Outdated Show resolved Hide resolved
@oxtoacart
Copy link
Contributor Author

This links to tailscale/corp#11854, which is probably the wrong issue. It would be great to see a description of the problem this PR solves for context (sorry if I missed it somewhere!)

Sorry, wrong repo. See #11854.

ssh/tailssh/incubator.go Outdated Show resolved Hide resolved
ssh/tailssh/incubator.go Outdated Show resolved Hide resolved
ssh/tailssh/incubator.go Outdated Show resolved Hide resolved
ssh/tailssh/incubator.go Outdated Show resolved Hide resolved
Comment on lines 393 to 451
// First try to execute su -l <user> -c id to make sure su supports the
// necessary arguments.
Copy link
Contributor

Choose a reason for hiding this comment

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

are there versions of su that don't support these arguments?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know about Linux, but my version of MacOS doesn't support -c.

SU(1)                                                                                                         General Commands Manual                                                                                                         SU(1)

NAME
     su – substitute user identity

SYNOPSIS
     su [-] [-flm] [login [args]]

ssh/tailssh/incubator.go Outdated Show resolved Hide resolved
Comment on lines 407 to 408
loginArgs = append(loginArgs, "-c", ia.cmdName)
loginArgs = append(loginArgs, ia.cmdArgs...)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need to pass the whole command+args to the -c flag:

Suggested change
loginArgs = append(loginArgs, "-c", ia.cmdName)
loginArgs = append(loginArgs, ia.cmdArgs...)
loginArgs = append(loginArgs, "-c", strings.Join(append([]string{ia.cmdName}, ia.cmdArgs...), " "))

from local testing:

❱ su -l awly -c echo test
Password:

❱ su -l awly -c "echo test"
Password:
test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interestingly enough, I can't get this to fail in my integration test because SSH already passes the entire command and arguments as a single command string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hah, on MacOS at least, /usr/bin/login suffers from the same issue.

➜  oss git:(ox/11854-3) ✗ sudo /usr/bin/login -f -pq -h 100.100.100.102 testuser /bin/zsh -c ls -l /tmp/sftptest.dat
Desktop		Documents	Downloads	Library		Movies		Music		Pictures	Public		sftptest.dat
➜  oss git:(ox/11854-3) ✗ sudo /usr/bin/login -f -pq -h 100.100.100.102 testuser /bin/zsh -c "ls -l /tmp/sftptest.dat"
-rw-r--r--@ 1 testuser  wheel  11 May  4 11:38 /tmp/sftptest.dat

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So yeah, long story short, we're okay here because the command and its args are already passed as a single string from the invocation of beincubator. I'll add a unit test that asserts this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I've added a test that runs "echo test". I've also refactored the test to make it clearer what commands we're calling where, and whether they have arguments or not.

ssh/tailssh/incubator.go Show resolved Hide resolved
ssh/tailssh/incubator.go Show resolved Hide resolved
@oxtoacart oxtoacart force-pushed the ox/11854-3 branch 5 times, most recently from 8748e47 to b2b1cf8 Compare May 4, 2024 19:09
// findSU attempts to find an su command which supports the -l and -c flags.
// This actually calls the su command, which can cause side effects like
// triggering pam_mkhomedir.
func findSU(logf logger.Logf, ia incubatorArgs) (string, bool) {
Copy link
Contributor

Choose a reason for hiding this comment

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

optional: the only return values here are "", false and su, true.
maybe this can just return a single string value, and the called checks if it's empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, changed.

ssh/tailssh/incubator.go Show resolved Hide resolved
Comment on lines +499 to +532
func dropPrivileges(logf logger.Logf, ia incubatorArgs) error {
return doDropPrivileges(logf, ia.uid, ia.gid, ia.gids)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: this wrapper seems redundant now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I kept it to avoid complicating the test code.

@oxtoacart oxtoacart requested a review from a team as a code owner May 15, 2024 15:48
oxtoacart and others added 13 commits May 15, 2024 21:30
This allows pam authentication to run for ssh sessions, triggering
automation like pam_mkhomedir. Note - this does not apply to SFTP,
only shells and remotely executed commands.

Updates #11854

Signed-off-by: Percy Wegmann <percy@tailscale.com>
Co-authored-by: Andrew Lytvynov <awly@tailscale.com>
Signed-off-by: Percy Wegmann <ox.to.a.cart@gmail.com>
Co-authored-by: Andrew Lytvynov <awly@tailscale.com>
Signed-off-by: Percy Wegmann <ox.to.a.cart@gmail.com>
Co-authored-by: Andrew Lytvynov <awly@tailscale.com>
Signed-off-by: Percy Wegmann <ox.to.a.cart@gmail.com>
Co-authored-by: Andrew Lytvynov <awly@tailscale.com>
Signed-off-by: Percy Wegmann <ox.to.a.cart@gmail.com>
Signed-off-by: Percy Wegmann <percy@tailscale.com>
Signed-off-by: Percy Wegmann <percy@tailscale.com>
Signed-off-by: Percy Wegmann <percy@tailscale.com>
Signed-off-by: Percy Wegmann <percy@tailscale.com>
…anyway

Signed-off-by: Percy Wegmann <percy@tailscale.com>
true being a shell built in is less likely to be overriden with
something wrong or malicious.

Signed-off-by: Percy Wegmann <percy@tailscale.com>
Signed-off-by: Percy Wegmann <percy@tailscale.com>
Signed-off-by: Percy Wegmann <percy@tailscale.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants