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

pam: Always Honor PAM_TTY if set #722

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

Conversation

3v1n0
Copy link
Collaborator

@3v1n0 3v1n0 commented Jan 10, 2025

In case a PAM tty is used, we should do our terminal checks against that
instead of the stdin value, since they may differ.

Do this check also to ensure that we can run the interactive CLI UI.

A follow-up of #720 (comment)

UDENG-5760

@3v1n0 3v1n0 requested a review from a team as a code owner January 10, 2025 16:36
@codecov-commenter
Copy link

codecov-commenter commented Jan 10, 2025

Codecov Report

Attention: Patch coverage is 80.00000% with 8 lines in your changes missing coverage. Please review.

Project coverage is 83.18%. Comparing base (36511cd) to head (a7726d0).
Report is 169 commits behind head on main.

Files with missing lines Patch % Lines
pam/internal/adapter/utils.go 76.66% 6 Missing and 1 partial ⚠️
pam/pam.go 87.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #722      +/-   ##
==========================================
- Coverage   83.43%   83.18%   -0.26%     
==========================================
  Files          83       88       +5     
  Lines        8689     8975     +286     
  Branches       74       74              
==========================================
+ Hits         7250     7466     +216     
- Misses       1111     1164      +53     
- Partials      328      345      +17     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@3v1n0 3v1n0 force-pushed the pam-tty-check-fix branch 3 times, most recently from 7ef2707 to a7726d0 Compare January 10, 2025 18:46
Comment on lines 86 to 107
tty, err := os.OpenFile(pamTty, os.O_RDWR, 0600)
if err != nil {
return nil, func() {}, err
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This will fail if PAM_TTY is set to $DISPLAY, which according to https://manpages.ubuntu.com/manpages/noble/en/man3/pam_set_item.3.html was done in the past:

PAM_TTY
The terminal name prefixed by /dev/ for device files. In the past, graphical X-based
applications used to store the $DISPLAY variable here, but with the introduction of
PAM_XDISPLAY this usage is deprecated.

Should we print a warning instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, we even just print a debug message in L105

isTerminalTtyOnce.Do(func() {
tty, cleanup, err := getPamTty(mTx)
if err != nil {
log.Debugf(context.TODO(), "Failed to open PAM TTY: %s", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be a warning?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I didn't want to be too pedantic here, since it's not a big deal on pam.ErrBadItem in other failures, but I can probably filter the errors we don't care out...

if tty == nil {
tty = os.Stdin
}
defer cleanup()
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer returning nil in getPamTty and doing

if cleanup != nil {
    defer cleanup()
}

here, because it's less surprising IMO


// We check the fd could be passed to x/term to decide if we should fallback to stdin
if tty.Fd() > math.MaxInt {
return nil, cleanup, fmt.Errorf("unexpected large PAM TTY fd: %d", tty.Fd())
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe do the cleanup here? It seems a bit unexpected that the caller needs to do cleanup when an error is returned.

3v1n0 added 2 commits January 17, 2025 19:53
In case a PAM tty is used, we should do our terminal checks against that
instead of the stdin value, since they may differ
In case PAM_TTY is set we should use that to check if we can run the
interactive CLI UI, and if it's set we should use it as the tea input
@3v1n0 3v1n0 force-pushed the pam-tty-check-fix branch from a7726d0 to 1fbcc79 Compare January 17, 2025 19:54
@3v1n0 3v1n0 requested a review from adombeck January 17, 2025 19:55
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.

3 participants